← All problems
Top K Frequent Elements
Given an integer array, return the k most frequent elements. Your algorithm's time complexity should be better than O(n log n) — think about which data structures combine well here.
Complexity
Time O(n), Space O(n)
Common pitfalls
- Sorting all frequencies O(n log n)
- Forgetting ties can exceed k (fine - return any k)
Your solution
Log in to save your code and track progress.