http://www.practice.geeksforgeeks.org/problem-page.php?pid=493

Sorting Elements of an Array by Frequency

Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.

If frequencies of two elements are same, print them in increasing order.

Input:

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.

Output:

Print each sorted array in a seperate line. For each array its numbers should be seperated by space.

Constraints:

1 ≤ T ≤ 70
30 ≤ N ≤ 130
1 ≤ A [ i ] ≤ 60

Example:

Input:

1
5
5 5 4 6 4

Output:

4 4 5 5 6

import java.util.*;
import java.lang.*;
import java.io.*; class pair {
public int freq;
public int key;
public pair(int f, int k) {
super();
this.freq = f;
this.key = k;
}
} class cmp implements Comparator<pair> { public int compare(pair p1, pair p2) {
if(p1.freq != p2.freq) {
return p2.freq - p1.freq;
} else {
return p1.key - p2.key;
}
}
} class GFG { public static void pln(Object[] ls) {
for(int i=0; i<ls.length; ++i) {
System.out.print(ls[i]);
} System.out.println();
} public static void func(int[] arr) { int n = arr.length;
HashMap<Integer, Integer> mapping = new HashMap<Integer, Integer> (); for(int na: arr) {
if(!mapping.containsKey(na)) {
mapping.put(na, 0);
}
mapping.put(na, mapping.get(na) + 1);
} TreeSet<pair> freqToKey = new TreeSet<pair> (new cmp());
Iterator iter = mapping.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
int key = (int) entry.getKey();
int freq = (int) entry.getValue();
pair p = new pair(freq, key);
freqToKey.add(p);
} Object[] ls = freqToKey.toArray();
for(int i=0; i<ls.length; ++i) {
pair p = (pair) ls[i];
int freq = p.freq;
int key = p.key; while(freq > 0) {
--freq;
System.out.print(key + " ");
}
} System.out.println();
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int times = in.nextInt(); while(times > 0) {
--times; int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; ++i) {
arr[i] = in.nextInt();
} func(arr);
}
}
}

geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)的更多相关文章

  1. Array类的Sort()方法

    刚复习了Array类的sort()方法, 这里列举几个常用的,和大家一起分享. Array类实现了数组中元素的冒泡排序.Sort()方法要求数组中的元素实现IComparable接口.如System. ...

  2. 【leetcode】1144. Decrease Elements To Make Array Zigzag

    题目如下: Given an array nums of integers, a move consists of choosing any element and decreasing it by ...

  3. 【LeetCode】1464. 数组中两元素的最大乘积 Maximum Product of Two Elements in an Array (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找最大次大 日期 题目地址:https://le ...

  4. 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)

    http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...

  5. [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript

    Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...

  6. .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)

    从string[]转List<string>: " }; List<string> list = new List<string>(str); 从List ...

  7. js中的数组Array定义与sort方法使用示例

    Array的定义及sort方法使用示例 Array数组相当于java中的ArrayList  定义方法:  1:使用new Array(5  )创建数组 var ary = new Array(5): ...

  8. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...

  9. 排序算法(sorting algorithm)之 插入排序(insertion sort)

    https://en.wikipedia.org/wiki/Insertion_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 loop2: 4,6,1,3,7 -> ...

随机推荐

  1. linux字符驱动程序结构

    linux内核为字符设备的驱动程序设计,提供了一些数据结构,和函数,供开发人员调用,将设备驱动程序注册到内核去.现代操作系统几乎都不直接和硬件通信,而是通过定义的接口,是硬件厂商自己来开发符合标准某个 ...

  2. win32窗口机制之CreateWindow

    CreateWindow     函数功能:该函数创建一个重叠式窗口.弹出式窗口或子窗口.它指定窗口类,窗口标题,窗口   风格,以及窗口的初始位置及大小(可选的).该函数也指定该窗口的父窗口或所属窗 ...

  3. smarty 初始配置文件

    <?php define("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); r ...

  4. 打印Dom对象的所有属性和方法

    <html> <head> <title>Test</title> <meta http-equiv="Content-Type&quo ...

  5. PHP基础 CGI,FastCGI,PHP-CGI与PHP-FPM

    CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...

  6. Spring无配置使用properties文件

    利用@PropertySource注解加载 @Configuration @ComponentScan(basePackages="*") @PropertySource({&qu ...

  7. poj2387 Til the Cows Come Home

    解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...

  8. poj 1986 Distance Queries

    好像是模板题  当作练习题 不错:  要求任意两点之间的距离.可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先:  距离就变成了 dis[u]+dis[v] - 2*dis[ ...

  9. Linux 设备驱动 Edition 3

    原文网址:http://oss.org.cn/kernel-book/ldd3/index.html Linux 设备驱动 Edition 3 By Jonathan Corbet, Alessand ...

  10. 自定义视图一:扩展现有的视图,添加新的XML属性

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! 简介 这个系列详细的介绍了如何穿件Android自定义视图.主要涉及的内容有如何绘制内容,layout和measure的原理,如何继承 ...