Algorithmsfrequency max tracking
Tracking Maximum Frequency
TT
Testlaa Team
May 14, 2026•1 min read
Track the maximum frequency while counts mutate: maintain best and update in O(1) when a single key increments—avoid scanning all keys each time when possible.
Why this shows up in the real world
Leaderboards keep top score; rate limiters track hottest keys in a time bucket.
Core idea (explained for students)
On cnt[x]+=1: if new count > best, update. On decrement affecting the current champion, re-scan or maintain a reverse index freq_to_keys for O(1) amortized advanced structures.
Try this in Python
from collections import Counter
def max_freq(nums: list[int]) -> int:
return max(Counter(nums).values()) if nums else 0
print(max_freq([1, 2, 2, 3, 3, 3]))
Common mistakes
- Assuming max is unique—ties may matter.
- Forgetting to drop keys at count 0 from auxiliary structures.
Key takeaways
- For static counts,
max(counter.values())is simplest. - For streaming, evaluate whether lazy recompute of max every k steps is acceptable.
Tags:
Hashing & frequencyPythonStudents
