Idea is very straightforward:

- build a counter map that maps a num to its frequency
- build a heap/priority queue that keeps track of
`k`

most significant entries - iterate through the final heap and get the keys

Code in Java:

```
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> counterMap = new HashMap<>();
for(int num : nums) {
int count = counterMap.getOrDefault(num, 0);
counterMap.put(num, count+1);
}
PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>((a, b) -> a.getValue()-b.getValue());
for(Map.Entry<Integer, Integer> entry : counterMap.entrySet()) {
pq.offer(entry);
if(pq.size() > k) pq.poll();
}
List<Integer> res = new LinkedList<>();
while(!pq.isEmpty()) {
res.add(0, pq.poll().getKey());
}
return res;
}
```