geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
2016-07-05 04:36
751 查看
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
View Code
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 ; for(int i=0; i<n; ++i) { arr[i] = in.nextInt(); } func(arr); } } }
View Code
相关文章推荐
- Longest Consecutive Sequence
- HTML标签marquee实现滚动效果
- APUE(5)---标准I/O库 (3)
- JS与HTML结合使用marquee标签实现无缝滚动效果代码
- UIScrollView截取内部内容存入相册
- UIPickerView
- iOS第三方左对齐布局类——UICollectionViewLeftAlignedLayout
- 使用Storyboard时出现以下警告:Scene is unreachable due to lack of entry points and does not have an identifier
- sqlite错误处理:library routine called out of sequence
- 第三十四篇:在SOUI中使用异步通知
- Android系统应用---SystemUI之二:Statusbar显示流程以及系统状态图标更新分析
- 微软极品工具箱-Sysinternals Suite
- UIScrollView增加刷新
- CDMA系统的UIM卡介绍 ,IMSI,ESN,MDN
- 消息队列(Message Queue)简介及其使用
- uuid_short() 源代码
- Android酷炫实用的开源框架(UI框架)
- 非GUI-Qt程序运行后显示Console(简单好用)
- deque 迭代器失效的问题详解
- The current branch is not configured for pull No value for key branch.master.merge found in configur