Top K Frequent Elements
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Heap / Priority Queue.
Top K Frequent Elements
Given an integer array
nums and an integer k, return the k most frequent elements. You may return the answer in any order.Visual Representation
nums = [1, 1, 1, 2, 2, 3], k = 2
Frequency Count Buckets:
0: []
1: [3] (val 3 appears 1 time)
2: [2] (val 2 appears 2 times)
3: [1] (val 1 appears 3 times)
4: []
5: []
6: []
Iterate buckets from right to left:
Bucket 3 -> [1]
Bucket 2 -> [2]
Result (k=2): [1, 2]Examples
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1, 2]
1 appears 3 times, 2 appears 2 times, 3 appears 1 time. Top 2 are [1, 2].
Input: nums = [1], k = 1
Output: [1]
Approach 1
Level I: Sorting (Naive)
Intuition
Count the frequency of each element and then sort the elements based on their count in descending order. Pick the first
k elements.Thought Process
- Use a Hash Map to count occurrences.
- Create a list of pairs
(element, frequency). - Sort the list by frequency in descending order.
- Take the top
kelements.
⏱ O(N log N) for sorting💾 O(N) to store frequencies
Detailed Dry Run
nums = [1, 1, 2], k = 1
- Map: {1: 2, 2: 1}
- Pairs: [(1, 2), (2, 1)]
- Sorted: [(1, 2), (2, 1)]
- result: [1]
Approach 2
Level II: Bucket Sort (Optimal)
Intuition
Since frequency is bounded by array length , we can use buckets where
bucket[i] stores elements that appear times. Iterate from down to 1.Thought Process
- Count frequencies with a Map.
- Create an array of lists
bucketsof size . - Place each element in its corresponding frequency bucket.
- Traverse buckets from right to left to collect top elements.
⏱ O(N)💾 O(N)
Detailed Dry Run
nums = [1, 1, 1, 2, 2, 3], k = 2
- Map: {1:3, 2:2, 3:1}
- Buckets: [[], [3], [2], [1], [], [], []]
- Bucket 3: adds [1]. Result [1]
- Bucket 2: adds [2]. Result [1, 2]. Done.
Approach 3
Level III: Min-Heap (Optimal)
Intuition
Instead of sorting all elements, we can maintain a Min-Heap of size
k. If the size exceeds k, we pop the element with the smallest frequency. At the end, the heap contains the k most frequent elements.Thought Process
- Count frequencies using a Map.
- Iterate through unique elements and push them into a Min-Heap based on frequency.
- If heap size >
k, pop from heap. - The remaining elements in the heap are the result.
⏱ O(N log K)💾 O(N) for frequency map
Detailed Dry Run
nums = [1, 1, 1, 2, 2, 3], k = 2
- Map: {1:3, 2:2, 3:1}
- Push 1 (freq 3): Heap [(1,3)]
- Push 2 (freq 2): Heap [(2,2), (1,3)]
- Push 3 (freq 1): Heap [(3,1), (2,2), (1,3)] -> Size 3 > k, Pop (3,1). Final Heap: [(2,2), (1,3)] -> Result [1, 2]
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.