8 1 1 1 2 2 3 3 4 2
[1, 2]
static int[] topK(int[] a,int k){java.util.HashMap<Integer,Integer> m=new java.util.HashMap<>();for(int x:a) m.put(x,m.getOrDefault(x,0)+1);java.util.PriorityQueue<int[]> pq=new java.util.PriorityQueue<>((u,v)->Integer.compare(u[1],v[1]));for(java.util.Map.Entry<Integer,Integer> e:m.entrySet()){pq.add(new int[]{e.getKey(),e.getValue()});if(pq.size()>k) pq.poll();}int[] res=new int[pq.size()];for(int i=res.length-1;i>=0;i--) res[i]=pq.poll()[0];return res;}
static int[] topK(int[] a,int k){java.util.HashMap<Integer,Integer> m=new java.util.HashMap<>();for(int x:a) m.put(x,m.getOrDefault(x,0)+1);java.util.PriorityQueue<int[]> pq=new java.util.PriorityQueue<>((u,v)->Integer.compare(u[1],v[1]));for(java.util.Map.Entry<Integer,Integer> e:m.entrySet()){pq.add(new int[]{e.getKey(),e.getValue()});if(pq.size()>k) pq.poll();}int[] res=new int[pq.size()];for(int i=res.length-1;i>=0;i--) res[i]=pq.poll()[0];return res;}