class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<>();
for(int item: nums){
map.put(item,map.getOrDefault(item,0)+1);
}
List<Map.Entry<Integer,Integer>> list =
new ArrayList<>(map.entrySet());
list.sort((o1,o2)->o2.getValue()-o1.getValue());
int [] res = new int [k] ;
int i = 0 ;
for(Map.Entry<Integer,Integer> item : list){
res[i] = item.getKey();
i ++ ;
if( i == k) break;
}
return res;
}
}