https://leetcode-cn.com/problems/top-k-frequent-elements/
class Solution {
public static void main(String[] args) {
new Solution().topKFrequent(new int[]{1, 1, 1, 2, 2, 3},1);
}
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++){
map.put(nums[i],map.get(nums[i])==null?1:map.get(nums[i])+1);
}
List<Map.Entry<Integer,Integer>> list = new ArrayList(map.entrySet());
Collections.sort(list, Comparator.comparingInt(Map.Entry::getValue));
int[] ans=new int[k];
for (int i=0,j=list.size()-1;i<k;i++,j--){
ans[i]=list.get(j).getKey();
}
return ans;
}
}