View all POTD Solutions
Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Heaps but will also help you build up problem-solving skills.
 POTD Solutions 5 Nov’ 2023
We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.
POTD 05 November: Top K Frequent Elements in Array
Given a non-empty array nums[] of integers of length N, find the top k elements which have the highest frequency in the array. If two numbers have same frequencies, then the larger number should be given more preference.
Example:
Input: N = 6, nums = {1,1,1,2,2,3}, k = 2 Output: {1, 2} Explanation: The most frequent element in nums[] is 1 and the second most frequent element is 2.
Input: N = 8, nums = {1,1,2,2,3,3,3,4}, k = 2 Output: {3, 2} Explanation: Element 3 is the most frequent. Elements 1 and 2 have the same frequency ie. 2. Therefore, in this case, the answer includes the element 2 as 2 > 1.
Create a Map to store element-frequency pair. Map is used to perform insertion and updates in constant time. Then use a priority queue to store the element-frequency pair (Max-Heap). The element which has maximum frequency, comes at the root of the Priority Queue. Remove the top or root of Priority Queue K times and print the element.
Steps-by-step approach:
- Create a map mp, to store key-value pair, i.e. element-frequency pair.
- Traverse the array from start to end.
- For every element in the array update mp[array[i]]++
- Store the element-frequency pair in a Priority Queue
- Run a loop k times, and in each iteration remove the root of the priority queue and print the element.
Below is the implementation of the above approach:
C++
class Solution {
public :
struct compare {
bool operator()(pair< int , int > p1,
pair< int , int > p2)
{
if (p1.second == p2.second)
return p1.first < p2.first;
return p1.second < p2.second;
}
};
vector< int > topK(vector< int >& nums, int k)
{
int n = nums.size();
vector< int > ans;
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[nums[i]]++;
priority_queue<pair< int , int >,
vector<pair< int , int > >, compare>
pq(mp.begin(), mp.end());
for ( int i = 1; i <= k; i++) {
ans.push_back(pq.top().first);
pq.pop();
}
return ans;
}
};
|
Java
class Solution {
public int [] topK( int [] nums, int k)
{
int n = nums.length;
int ans[] = new int [k];
Map<Integer, Integer> mp
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
mp.put(nums[i],
mp.getOrDefault(nums[i], 0 ) + 1 );
}
PriorityQueue<Map.Entry<Integer, Integer> > queue
= new PriorityQueue<>(
(a, b)
-> a.getValue().equals(b.getValue())
? Integer.compare(b.getKey(),
a.getKey())
: Integer.compare(b.getValue(),
a.getValue()));
for (Map.Entry<Integer, Integer> entry :
mp.entrySet())
queue.offer(entry);
for ( int i = 0 ; i < k; i++) {
ans[i] = queue.poll().getKey();
}
return ans;
}
}
|
Python3
import heapq
class Solution:
def topK( self , nums, k):
mp = dict ()
ans = [ 0 ] * k
n = len (nums)
for i in range ( 0 , n):
if nums[i] not in mp:
mp[nums[i]] = 0
else :
mp[nums[i]] + = 1
heap = [(value, key) for key,
value in mp.items()]
largest = heapq.nlargest(k, heap)
for i in range (k):
ans[i] = largest[i][ 1 ]
return ans
|
Javascript
class Solution {
topK(nums, k) {
let mp = new Map();
let ans = new Array(k);
let n = nums.length;
for (let i = 0; i < n; i++) {
if (!mp.has(nums[i]))
mp.set(nums[i],0);
mp.set(nums[i],
mp.get(nums[i]) + 1);
}
let queue=[...mp];
queue.sort( function (a,b){
if (a[1]==b[1])
{
return b[0]-a[0];
}
else
{
return b[1]-a[1];
}
});
for (let i=0; i<k; i++)
{
ans[i] = queue[i][0];
}
return ans;
}
}
|
Time Complexity: O(K log D + D log D), where D is the count of distinct elements in the array. Auxiliary Space: O(D), where D is the count of distinct elements in the array.
|