-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
30 lines (30 loc) · 1.03 KB
/
Solution.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
public class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer,Integer> map1=new HashMap<Integer, Integer>();
Map<Integer,List<Integer>> map2=new HashMap<Integer, List<Integer>>();
for (int i = 0; i < nums.length; i++) {
map1.put(nums[i], map1.get(nums[i])==null?1:(map1.get(nums[i])+1));
}
Set<Integer> set=map1.keySet();
int[] arrays=new int[set.size()];
int a=0;
for (Integer key : set) {
int value=map1.get(key);
if (map2.get(value)==null) {
List<Integer> list=new ArrayList<Integer>();
list.add(key);
map2.put(value, list);
arrays[a++]=value;
}else {
map2.get(value).add(key);
}
}
Arrays.sort(arrays);
List<Integer> list=new ArrayList<Integer>();
a=arrays.length-1;
while (list.size()<k) {
list.addAll(map2.get(arrays[a--]));
}
return list;
}
}