Find Top K Frequent Elements |3 Approaches

Find top k frequent elements in an array of integers. Let’s first understand the problem statement and the we will solve this problem using multiple approaches.

Given a non-empty array of integers, return the k most frequent elements.

Example 1:

Input: arr = {3, 4, 4, 4, 7, 7}, k = 2

Output: {4, 7}

Two most frequent elements are 4 and 7.

Example 2:

Input: arr = {3}, k = 1

Output: {3}

NOTE:

* k is always valid, 1 ≤ k ≤ number of unique elements.

* The time complexity must be better than O(nlogn), where n is the array’s size.

* It’s guaranteed that the answer is unique, in other words, the set of the top k frequent elements is unique.

* Answer can be returned in any order.

We have discussed the problem statement. How do we approach this problem? In this tutorial, I am going to explain three approaches and it’s java code to solve this problem.

Top K Frequent Elements Video Tutorial

Programming Video Tutorials

Sort an array of 0s, 1s and 2s

Find first and last position of a number in a sorted array

Find Top K Most Frequent Numbers by using HashMap – Java Code

The idea here is to first traverse an array and create a map of all numbers and their count. The ideal data structure for storing key and value pairs is HashMap. The next step is to sort the HashMap by its value and pick only k elements.

The time complexity of this approach is O(nlogn) and its space complexity is O(n).

Find Top K Frequent Elements Solution using Priority Queue

Let’s improve our previous solution using a priority queue. To solve this problem first create a map of each element and its count. Then use a priority queue (Min Heap) and we keep its size equal to k.

Take all the keys of a map and put them in a Priority Queue. If the size of a priority queue is greater than k, In that case, we poll the top element which is the minimum element. Once all the elements of HashMap are put in a Priority queue, we only get the top k elements.

The time complexity of this approach is O(nlogk) and its space complexity is O(n).

Find Top K Frequent Elements – Java Code

In this example, Let’s discuss how we can solve this problem using bucket sort, and then we will write its java code.

Here are the following steps to solve this problem.

i) To find the most frequent elements, First, we need to know the number and its count. For this, we can use HashMap to store the number and its count.

So, if the input array is {1, 1, 1, 2, 2, 3, 3, 3} then at the end of this operation map would look like this: 1 -> 3, 2 -> 2, 3 -> 3.

We also need to keep track of maximum frequency, so its value, in this case, would be 3.

Sorting algorithms and their time complexities

ii) Then in the next step, create a list. At each index, we can store multiple elements. Its size would be maximum frequency + 1.

iii) Based on the frequency of a number put the number in the appropriate bucket. There might be more than one number with the same frequency, so we have to put them in the same bucket.

iv) Now, iterate over the bucket elements and print the k most frequent elements.

The time complexity of this approach is O(n).

I have also explained this approach using a video tutorial. The link to that video tutorial is present at the end of this post.

In this video tutorial, I have explained how we can solve this problem efficiently using map data structure and bucket sort.

Find top k most frequent elements video tutorial

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.