← All problems

Top K Frequent Elements

Medium heapshash mapsbucket-sort LeetCode ↗
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.

Hints (0/3 revealed)

Log in to reveal hints progressively.

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.