Problem
Given a non-empty array of integers, return the k
most frequent elements.
Example
Given [1,1,1,2,2,3]
and k = 2
, return [1,2]
.
Note
You may assume k
is always valid, 1 ≤ k ≤ number of unique elements
.
Your algorithm's time complexity must be better than O(n log n)
, where n
is the array's size.
Solution
public class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
int n = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for (int num: nums) {
if (map.containsKey(num)) map.put(num, map.get(num)+1);
else map.put(num, 1);
}
List<Integer>[] freq = new ArrayList[n+1];
for (int num : map.keySet()) {
int i = map.get(num);
if (freq[i] == null) {
freq[i] = new ArrayList<>();
}
freq[i].add(num);
}
List<Integer> res = new ArrayList<>();
for (int index = freq.length - 1; index >= 0 && res.size() < k; index--) {
if (freq[index] != null) {
res.addAll(freq[index]);
}
}
return res;
}
}