Back to problems
← Contains Duplicate Valid Anagram →

Character Counting

Most Frequent Character — Hash Map

Easy • Arrays & Hashing

Given a string s, count the frequency of each character and return the most frequent character. If there's a tie, return the one that appears first.

TimeO(n)two passes
SpaceO(k)hash map, k = unique chars
Phase: Index: Result:
Current index
In hash map
Best so far
map
result
maxCount
Ready
Press Play to watch. Pass 1: count each character in a hash map. Pass 2: scan left-to-right, return first char with max count.