You're given an integer array nums and an integer k. Your task is to find the k most frequently occurring elements in the array and return them.

Goal: Identify which elements appear most often and return exactly k of them.

Example: In array [1,1,1,2,2,3] with k=2, element 1 appears 3 times and element 2 appears 2 times, so return [1,2].

Note: The answer can be returned in any order, and it's guaranteed that the answer is unique.

Input & Output

example_1.py โ€” Basic case with clear frequencies
$ Input: nums = [1,1,1,2,2,3], k = 2
โ€บ Output: [1,2]
๐Ÿ’ก Note: Element 1 appears 3 times (most frequent), element 2 appears 2 times (second most frequent), and element 3 appears 1 time. So the top 2 most frequent elements are 1 and 2.
example_2.py โ€” Single element array
$ Input: nums = [1], k = 1
โ€บ Output: [1]
๐Ÿ’ก Note: There's only one unique element, so it must be the most frequent (and only) element to return.
example_3.py โ€” All elements have same frequency
$ Input: nums = [1,2,3,4,5], k = 3
โ€บ Output: [1,2,3] (or any 3 elements)
๐Ÿ’ก Note: All elements appear exactly once, so any 3 elements form a valid answer since they all have the same frequency.

Visualization

Tap to expand
๐ŸŽต Top K Music Chart AlgorithmStep 1: Song Play Counter (Hash Map)Play History: [๐ŸŽต1,๐ŸŽต1,๐ŸŽต1,๐ŸŽต2,๐ŸŽต2,๐ŸŽต3]๐Ÿ“Š Song Counter:๐ŸŽต1: โ–ˆโ–ˆโ–ˆ (3 plays)๐ŸŽต2: โ–ˆโ–ˆ (2 plays)๐ŸŽต3: โ–ˆ (1 play)Step 2: Top K=2 Chart (Min Heap)๐Ÿ† Current Top 2 Chart:๐ŸŽต22 plays (min)๐ŸŽต13 playsStep 3: Smart Filtering ProcessProcessing ๐ŸŽต3 (1 play):๐ŸŽต2Min: 2 plays๐ŸŽต3 has 1 play < 2โŒ Don't add to chart๐ŸŽต3Rejected!Step 4: Final Chart Result๐Ÿ† Top K=2 Most Played:#1: ๐ŸŽต1#2: ๐ŸŽต2Result: [1, 2]๐Ÿ’ก Algorithm Insights:โœ“ Hash Map: O(n) countingโœ“ Min Heap: Only k elementsโœ“ No full sorting needed!โฑ๏ธ Time: O(n log k)๐Ÿ’พ Space: O(n + k)๐Ÿš€ Optimal when k << n๐ŸŽฏ Perfect for top charts!
Understanding the Visualization
1
Count Song Plays
Like a radio station tracking how many times each song was played - use a hash map
2
Maintain Top K Chart
Instead of sorting all songs, use a min heap to efficiently maintain just the top k songs
3
Smart Filtering
When a new song is processed, only keep it if it's more popular than the least popular in our top k
4
Final Chart
Extract the k songs from our chart - these are the most frequent elements
Key Takeaway
๐ŸŽฏ Key Insight: Instead of sorting ALL songs by popularity (expensive), we maintain a small chart of just the top k songs using a min heap, making it perfect for scenarios where k is much smaller than the total number of unique elements!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log k)

O(n) to build frequency map + O(n log k) for heap operations (at most k elements in heap)

n
2n
โšก Linearithmic
Space Complexity
O(n + k)

O(n) for frequency map + O(k) for heap of size k

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • k is in the range [1, number of unique elements in the array]
  • -104 โ‰ค nums[i] โ‰ค 104
  • It is guaranteed that the answer is unique
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
42.8K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen