Problem Description
Given a non-empty integer array and an integer k, return the k most frequent elements. The problem guarantees that there is a unique answer and that the order of the returned elements can be arbitrary.
Key Insights
- Count the frequency of each element using a hash table.
- Use bucket sort to achieve linear time complexity by grouping numbers by their frequency.
- Traverse the frequency buckets from highest to lowest to collect the k most frequent elements.
- Alternative approaches include using heaps or Quickselect, but bucket sort meets the follow-up requirement of better than O(n log n) time complexity.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the array (bucket sort and frequency counting are linear). Space Complexity: O(n) for storing the counts and buckets.
Solution
The solution starts by counting the frequency of each element with a hash table (or dictionary). Then, we create an array of buckets where the index represents the frequency. Each bucket holds the numbers that appear with that frequency. Finally, we iterate over the buckets in reverse order (from high frequency to low) and collect elements until we have k of them. This bucket sort approach avoids the higher complexity of sorting a list of frequencies and guarantees linear time performance.