@@ -0,0 +1,1574 @@
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Comparator;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Apublic class Solution %7B%0A    private Map%3CInteger, Integer%3E mp = new HashMap%3CInteger, Integer%3E();%0A%0A    public List%3CInteger%3E topKFrequent(int%5B%5D nums, int k) %7B%0A        mp.clear();%0A        for (int i = 0; i %3C nums.length; ++i) %7B%0A            if (mp.containsKey(nums%5Bi%5D)) %7B%0A                mp.put(nums%5Bi%5D, mp.get(nums%5Bi%5D) + 1);%0A            %7D else %7B%0A                mp.put(nums%5Bi%5D, 1);%0A            %7D%0A        %7D%0A        Node%5B%5D st = new Node%5Bmp.size()%5D;%0A        int cnt = 0;%0A        for (Map.Entry%3CInteger, Integer%3E entry : mp.entrySet()) %7B%0A            Node node = new Node();%0A            node.num = entry.getKey();%0A            node.times = entry.getValue();%0A            st%5Bcnt++%5D = node;%0A        %7D%0A        Arrays.sort(st, new Comparator%3CNode%3E() %7B%0A            @Override%0A            public int compare(Node o1, Node o2) %7B%0A                return o1.times != o2.times ? o2.times - o1.times : o1.num - o2.num;%0A            %7D%0A        %7D);%0A        List%3CInteger%3E res = new ArrayList%3CInteger%3E();%0A        for (int i = 0; i %3C cnt && i %3C k; ++i) %7B%0A            res.add(st%5Bi%5D.num);%0A        %7D%0A        return res;%0A    %7D%0A%0A    class Node %7B%0A        public int num;%0A        public int times;%0A    %7D%0A%0A    public void main() %7B%0A        int%5B%5D nums = new int%5B%5D %7B 1, 1, 1, 2, 2, 3 %7D;%0A        int k = 2;%0A        System.out.println(new Solution().topKFrequent(nums, k));%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        new Solution().main();%0A    %7D%0A%7D

solve top-k-frequent-elements for java version