VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-k-numbers-occurrences-given-array/

⇱ Top K Frequent in an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Top K Frequent in an Array

Last Updated : 18 Jan, 2026

Given an array arr[] and a positive integer k, Find the top k elements which have the highest frequency in the array.

Note: If more than one element has same frequency then priorities the larger element over the smaller one.

Examples:

Input: arr[] = [3, 1, 4, 4, 5, 2, 6, 1], k = 2
Output: [4, 1]
Explanation: Frequency of 4 is 2 and frequency of 1 is 2, these two have the maximum frequency.

Input: arr[] = [7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9], k = 4
Output: [5, 11, 7, 10]
Explanation: Frequency of 5 is 3, frequency of 11 is 2, frequency of 7 is 2, and frequency of rest is 1 but 10 is largest .

[Naive Approach] Using Hash map and Sorting

The idea is to use a hashmap to store each element along with its frequency, allowing insertion and updates in constant time. After building the frequency map, sort the elements in decreasing order of their frequency. To find the top k elements, simply take the first k elements from the sorted list.


Output
4 1 

Time Complexity: O(n +d*log d), where n is the size of the array and d is the count of distinct elements in the array.
Auxiliary Space: O(d)

[Expected Approach 1] Using Hash map and Min Heap

The idea is to use a hashmap to store each element and its frequency. Then, use a priority queue (min-heap) to store pairs of frequency and element, so that the element with the smallest frequency is on top. Iterate through the hashmap and push each pair into the heap, and if the heap size exceeds k, remove the top element. After processing all elements, the heap will contain the k most frequent elements. Finally, extract these elements from the heap and store them in the result array.


Output
4 1 

Time Complexity: O(n + k*log k ), where n is the size of array.
Auxiliary Space: O(d), where d is the count of distinct elements in the array. 

[Expected Approach 2] Using Counting sort - O(n log n) Time and O(n) Space

The idea is to utilise more space to improve the time complexity, we store the elements based on their frequencies. We can use the frequency of each element as index of 2D array, where each index represents a list of elements of specific frequency. By doing this, we reduce the need for complex sorting operations. Instead, we can efficiently traverse the buckets from highest frequency to lowest and collect the top k most frequent elements.


Output
4 1 

[Alternate Approach] Using Quick Select

The idea is to use QuickSelect to find the top k frequent elements by partitioning the array based on element frequency. We pick a pivot and place all elements with higher frequency to its left and lower frequency to its right, putting the pivot in its correct position. We then recursively focus only on the part that can contain the k-th most frequent element.


Output
4 1 

Time complexity:  O(n + d^2), the QuickSelect algorithm takes O(d log d) time on average and works faster than other O(d log d) algorithms in practice.
AuxiliarySpace: O(n)

Comment