AlgoMaster Logo

Top K Frequent Elements

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We have an array of integers that may contain duplicates, and we need to find which elements appear most often. We want the top k by frequency. The order of the output doesn't matter, which gives flexibility in how we collect results.

There are two steps: counting how often each element appears, then selecting the k elements with the highest counts. Counting is straightforward with a hash map. The second step is where the approaches differ, because how we select the top k determines whether the solution is O(n log n), O(n log k), or O(n).

Key Constraints:

  • 1 <= nums.length <= 10^5 → With n up to 100,000, O(n log n) passes, but the follow-up asks us to beat it. This points toward O(n log k) or O(n) solutions.
  • k is valid and the answer is unique → We don't need to handle ties or invalid k values.

Approach 1: Sort by Frequency

Intuition

Count how often each element appears, then sort the unique elements by frequency and take the top k.

A hash map counts frequencies in one pass. We then sort the unique elements by their frequency in descending order. The first k elements in the sorted list are the answer.

Algorithm

  1. Build a hash map freq where each key is an element from nums and the value is its count.
  2. Collect all unique elements (the keys of the map).
  3. Sort these unique elements by their frequency in descending order.
  4. Return the first k elements from the sorted list.

Example Walkthrough

nums
1Start: count frequency of each element
0
1
i
1
1
2
1
3
2
4
2
5
3
freq
1Start counting frequencies
1/4

Code

This sorts all unique elements when we only need the top k. The next approach tracks only k elements at a time, lowering the cost of the selection step.

Approach 2: Min-Heap

Intuition

Instead of sorting all unique elements, maintain a min-heap of size k. The heap holds the k most frequent elements seen so far. When a new element arrives, compare its frequency to the smallest frequency in the heap. If it's larger, evict the minimum and insert the new one.

A min-heap, not a max-heap, is the right choice here. The minimum frequency among the current top k candidates sits at the top of a min-heap, so removing the weakest candidate is an O(log k) operation. Any element still in the heap after processing every unique element has a frequency at least as high as every element that was evicted, so the final heap contents are the k most frequent.

Algorithm

  1. Build a frequency map from nums.
  2. Create a min-heap that orders elements by their frequency.
  3. For each unique element, add it to the heap. If the heap size exceeds k, remove the minimum.
  4. After processing all unique elements, the heap contains exactly the k most frequent elements.
  5. Extract all elements from the heap into the result array.

Example Walkthrough

nums
1Step 1: Build frequency map by scanning all elements
0
1
i
1
1
2
1
3
2
4
2
5
3
minHeap
1Min-heap is empty, building frequency map first
[]
1/6

Code

Every unique element still costs a log k heap operation. The final approach removes the logarithmic factor by bucketing elements directly by their frequency.

Approach 3: Bucket Sort

Intuition

The maximum frequency any element can have is n, the length of the array. So we can create an array of buckets where bucket[i] holds all elements that appear exactly i times. Walking from the highest bucket down and collecting elements gives the most frequent ones first.

This eliminates sorting and heap operations. The bucket array has size n+1, placing an element into its bucket is O(1), and walking the buckets from the end visits at most n+1 indices. The whole thing runs in O(n) time.

Algorithm

  1. Build a frequency map from nums.
  2. Create an array buckets of size n+1, where buckets[i] is a list of elements with frequency i.
  3. Place each unique element into its corresponding bucket based on its frequency.
  4. Iterate from the highest index (n) down to 1. For each non-empty bucket, add its elements to the result.
  5. Stop once we've collected k elements.

Example Walkthrough

nums
1Step 1: Build frequency map by scanning nums
0
1
i
1
1
2
1
3
2
4
2
5
3
buckets
1Create bucket array of size n+1=7 (index = frequency)
0
1
2
3
4
5
6
1/5

Code