High interview relevanceRoadmap: Beginner
TimeExpected O(n) total
SpaceO(n)

Why it costs this much

One pass over the input, with an expected O(1) dictionary update for each item.

Code

from collections import Counter
counts = Counter(items)               # one pass, expected O(1) per update
most_common = counts.most_common(1)   # scans the k distinct keys: O(k)

Common pitfalls

The bound is expected, not guaranteed, because it relies on hashing. Sorting items by frequency afterward costs O(n log n).

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list