Given an integer array nums
and an integer k
, return the k
most frequent elements. You may return the answer in any order.
For example:
nums = [1,1,1,2,2,3], k = 2
should return [1,2]
or [2,1]
nums = [1], k = 1
should return [1]
Your solution should meet the following constraints:
1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4
k
is in the range [1, the number of unique elements in the array]
.Follow-up: Can you design an algorithm with a time complexity better than O(n log n)
?
A naive approach involves counting the frequency of each element in the array and then sorting the elements based on their frequencies. We can then select the top k
elements.
k
elements from the sorted list.Code Example (Python):
def topKFrequent_brute_force(nums, k):
freq_map = {}
for num in nums:
freq_map[num] = freq_map.get(num, 0) + 1
sorted_nums = sorted(freq_map.items(), key=lambda x: x[1], reverse=True)
result = [num for num, freq in sorted_nums[:k]]
return result
Time Complexity: O(n log n), where n is the number of elements in the array. This is due to the sorting step.
Space Complexity: O(n), where n is the number of unique elements in the array, as we store the frequencies in a hash map.
An efficient solution can be achieved using a min-heap (priority queue). We maintain a heap of size k
that stores the k
most frequent elements seen so far. For each new element, we compare its frequency with the minimum frequency in the heap. If the new element's frequency is greater, we replace the element with the minimum frequency in the heap with the new element.
k
with elements from the frequency map. The heap will be ordered by frequency.k
most frequent elements. Extract them from the heap.Code Example (Python):
import heapq
def topKFrequent(nums, k):
freq_map = {}
for num in nums:
freq_map[num] = freq_map.get(num, 0) + 1
heap = []
for num, freq in freq_map.items():
if len(heap) < k:
heapq.heappush(heap, (freq, num))
elif freq > heap[0][0]:
heapq.heapreplace(heap, (freq, num))
result = [num for freq, num in heap]
return result
Time Complexity: O(n log k), where n is the number of elements in the array. Building the frequency map takes O(n) time. Iterating through the frequency map and heapifying takes O(n log k) time. If k is close to n, time complexity becomes O(n log n).
Space Complexity: O(n), where n is the number of unique elements in the array. This is for storing the frequency map.
nums
is empty, return an empty list.k
is 0, return an empty list.k
is greater than the number of unique elements, return all unique elements.