40ms using LinkedList HashMap in Java


  • 0
    S
    public static List<Integer> topKFrequent(int[] nums, int k) {
    List<Integer> res = new LinkedList<Integer>();
    HashMap<Integer, Integer> freq = new HashMap<>();
    for (int num : nums) {
    	freq.put(num, freq.getOrDefault(num, 0) + 1);
    
    }
    HashMap<Integer, LinkedList<Integer>> f = new HashMap<>();
    for (Map.Entry<Integer, Integer> m : freq.entrySet()) {
    	int key = m.getKey();
    	int value = m.getValue();
    	LinkedList<Integer> l = f.getOrDefault(value, new LinkedList<>());
    	l.add(key);
    	f.put(value, l);
    
    }
    for (int i = nums.length; i > 0 && k > 0; i--)
    	if (f.containsKey(i)) {
    		LinkedList<Integer> l = f.get(i);
    		for (Integer in1 : l)
    			if (k > 0) {
    				res.add(in1);
    				k--;
    			} else
    				break;
    	}
    return res;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.