-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFreqSort.java
More file actions
62 lines (48 loc) · 1.42 KB
/
FreqSort.java
File metadata and controls
62 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
import java.lang.*;
import java.io.*;
class FreqSort{
public static <K, V extends Comparable<V> > TreeMap<K, V> valueSort(final TreeMap<K, V> map)
{
Comparator<K> valueComparator = new Comparator<K>() {
public int compare(K k1, K k2)
{
int comp = map.get(k1).compareTo(map.get(k2));
if (comp == 0)
return 1;
else
return -1*comp;
}
};
TreeMap<K, V> sorted = new TreeMap<K, V>(valueComparator);
sorted.putAll(map);
return sorted;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int i,j;
int n = sc.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for(i=0;i<n;i++){
list.add(sc.nextInt());
}
TreeMap<Integer,Integer> map = new TreeMap<>();
for(i=0;i<n;i++){
map.put(list.get(i),(map.getOrDefault(list.get(i),0)+1));
}
TreeMap sortedMap = valueSort(map);
map = sortedMap;
int k,v;
for(Map.Entry<Integer, Integer> e: map.entrySet()){
k = e.getKey();
v = e.getValue();
for(i=0;i<v;i++){
System.out.print(k+" ");
}
}
System.out.println();
}
}
}