347. Top K Frequent Elements LeetCode Solution
In this guide, you will get 347. Top K Frequent Elements LeetCode Solution with the best time and space complexity. The solution to Top K Frequent Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Top K Frequent Elements solution in C++
- Top K Frequent Elements solution in Java
- Top K Frequent Elements solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
k is in the range [1, the number of unique elements in the array].
It is guaranteed that the answer is unique.
Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
Complexity Analysis
- Time Complexity: O(n\log k)
- Space Complexity: O(n)
347. Top K Frequent Elements LeetCode Solution in C++
struct T {
int num;
int freq;
};
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
const int n = nums.size();
vector<int> ans;
unordered_map<int, int> count;
auto compare = [](const T& a, const T& b) { return a.freq > b.freq; };
priority_queue<T, vector<T>, decltype(compare)> minHeap(compare);
for (const int num : nums)
++count[num];
for (const auto& [num, freq] : count) {
minHeap.emplace(num, freq);
if (minHeap.size() > k)
minHeap.pop();
}
while (!minHeap.empty())
ans.push_back(minHeap.top().num), minHeap.pop();
return ans;
}
};
/* code provided by PROGIEZ */
347. Top K Frequent Elements LeetCode Solution in Java
class T {
public int num;
public int freq;
public T(int num, int freq) {
this.num = num;
this.freq = freq;
}
}
class Solution {
public int[] topKFrequent(int[] nums, int k) {
final int n = nums.length;
int[] ans = new int[k];
Map<Integer, Integer> count = new HashMap<>();
Queue<T> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a.freq, b.freq));
for (final int num : nums)
count.merge(num, 1, Integer::sum);
for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
final int num = entry.getKey();
final int freq = entry.getValue();
minHeap.offer(new T(num, freq));
if (minHeap.size() > k)
minHeap.poll();
}
for (int i = 0; i < k; ++i)
ans[i] = minHeap.poll().num;
return ans;
}
}
// code provided by PROGIEZ
347. Top K Frequent Elements LeetCode Solution in Python
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
const int n = nums.size();
vector<int> ans;
vector<vector<int>> bucket(n + 1);
unordered_map<int, int> count;
for (const int num : nums)
++count[num];
for (const auto& [num, freq] : count)
bucket[freq].push_back(num);
for (int freq = n; freq > 0; --freq) {
for (const int num : bucket[freq])
ans.push_back(num);
if (ans.size() == k)
return ans;
}
throw;
}
};
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.