-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leet.java
59 lines (49 loc) · 1.73 KB
/
Leet.java
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
package com.org.let;
import java.util.*;
// find the frequency of elemnt and the sort the array according to the frequency of element.
public class Leet {
public static void main(String[] args) {
sort(new int[]{1,1,2,2,2,3});
sort(new int[]{2,3,1,3,2});
sort(new int[]{-1,1,-6,4,5,-6,1,4,1});
}
public static void sort(int[] arr){
HashMap<Integer, Integer> map = new HashMap<>();
for (int i : arr){
if(!map.isEmpty() ){
if(map.containsKey(i)){
map.put(i,map.get(i) + 1);
}else{
map.put(i,1);
}
}else{
map.put(i,1);
}
}
List<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
if(o1.getValue() == o2.getValue()){
return -1;
}
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
int idx = 0;
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
for (int i = 0 ; i < aa.getValue() ; i++ ) {
arr[idx] = aa.getKey();
idx+=1;
}
}
for(Map.Entry<Integer,Integer> set : temp.entrySet()){
System.out.println(set.getKey()+"--"+set.getValue());
}
for(int a : arr){
System.out.println(a);
}
}
}