05 - Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements in the array.
The test cases are generated such that the answer is always unique.
You may return the output in any order.
Examples¶
Example 1
Input
Output
Example 2
Input
Output
[7]
````
## Solution
```java
import java.util.*;
class Solution {
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
PriorityQueue<Integer> pq = new PriorityQueue<>(
(a, b) -> map.get(b) - map.get(a)
);
pq.addAll(map.keySet());
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = pq.poll();
}
return result;
}
}
````
## Intuition
The problem asks for the elements that appear most frequently.
Steps:
1. Count the frequency of every number.
2. Store these frequencies in a structure that can quickly give the highest frequency element.
3. Extract the top `k` elements.
Example
Order does not matter.
## Java Collections Framework Components Used
### HashMap
Used to store frequency of each number.
Structure
This ensures the element with the **highest frequency comes first**.
Important methods
`addAll(collection)`
Adds all keys into the heap.
`poll()`
Removes and returns the element with highest priority.
## Complexity Analysis
Let
Used by the hashmap and priority queue.
Key Takeaways¶
-
First count frequencies using
HashMap. -
Use
PriorityQueueto retrieve highest frequency elements. -
This combination of
HashMap + Heapis a common interview pattern.