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. [Topcoder]ZigZag(dp)

    题目链接:https://community.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 题意:给一串数字,求出最长的波 ...

  2. hadoop优点和缺点

    l扩容能力(Scalable):能可靠地(reliably)存储和处理千兆字节(PB)数据. l成本低(Economical):可以通过普通机器组成的服务器群来分发以及处理数据.这些服务器群总计可达数 ...

  3. Android微信SDK API 调用教程1

    最近一直在调用微信的API,却发现一直调用不成功,纠结了好久,各方面找教程,找官方,官方里的文档也只是写得很模糊,说是按三步走. 1.申请App_ID 2.填写包名3. 获取程序签名的md5值, 这三 ...

  4. [ionic开源项目教程] - 第7讲 实现下拉刷新上拉加载ion-refresher和ion-infinite-scroll

    第7讲 实现下拉刷新上拉加载ion-refresher和ion-infinite-scroll 1.将tab1.html的代码改为如下: <ion-content> <ion-ref ...

  5. 谷歌的ajax.googleapis.com被墙导致访问很多国外网站很慢的解决方法

    比如访问StackOverflow, 更比如flexerasoftware.com(导致Visual Studio的打包程序InstallShield Limited Edition不能注册和下载) ...

  6. 关闭oom killer

    最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通常是因为某时刻应用程序大量请求内存导致系统 ...

  7. 内核打上yaffs2补丁遇到的问题

    移植yaffs2文件系统时,首先要在内核中添加对yaffs2的支持,使用命令:./patch-ker.sh c 内核目录时,出现下面错误: usage:  ./patch-ker.sh  c/l m/ ...

  8. hihoCoder hiho一下 第四十八周 题目1 : 拓扑排序·二

    题意: 给定一个拓扑图,其中部分结点含有1个病毒,每个结点只要收到病毒就会立即往出边所能到达的点传播,病毒数可叠加,求所有结点的病毒数总和. 思路: 根据拓扑的特点,每个入度为0的点肯定不会再被传播病 ...

  9. Python [Leetcode 342]Power of Four

    题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...

  10. 【转】strcpy溢出的攻击示例

    在学习c/c++的时候,就讲到了一些C类型的字符串函数不是安全的,比如strcpy没有检查长度会溢出,推荐使用strncpy,笔试面试也经常问到.同时经常浏览安全相关的新闻,缓冲区溢出攻击是很常见的一 ...