Problems / Arrays & Hashing / Top K Frequent Elements
#5 Medium Time O(n) Space O(n) Animated

Top K Frequent Elements

Return the k most frequent elements in an array.

LeetCode ↗
Tutor Mode Build the mental model before you memorize the code.
  1. 1 Predict
  2. 2 Trace
  3. 3 Explain
  4. 4 Implement
Pattern Hashing for constant-time memory

Trade one linear scan for a small lookup structure so each value is handled once.

Approach: Bucket Sort
Click play to start
Step 0 / 0
Press play to start the visualization.

Steps