Given a string, return the character that appears most frequently, as a one-character string. If several characters tie for the highest count, return the one that appears first in the string.
mostFrequentChar(s)
mostFrequentChar("abccccdd") // → "c"
mostFrequentChar("hello") // → "l"
mostFrequentChar("aaa") // → "a"
mostFrequentChar("xyzx") // → "x"
You need two things at once: which character shows up most, and — if there's a tie — which of the tied characters appeared first. Tracking both in a single pass is awkward, since you don't know a character's final count until you've seen the whole string.
If you scan left to right and keep a running "champion," you can't compare a new character's count so far against the champion's final count — you don't know the final count yet. Better to separate the two jobs: first get the complete frequency of every character, then figure out ordering separately.
Because pass two scans in original order, and you only replace your best on a strictly greater count, the first character to reach the max frequency keeps its spot. A later character with the same max count only ties — it never beats it, so it can't overwrite the earlier one. "Earliest wins ties" falls out automatically, no extra logic required.
Each pass touches the string once, so two passes of size n is still O(n), not something worse. The only extra memory is the frequency map — at most one entry per distinct character. Compare that to rescanning the whole string for every character, which costs O(n²) in the worst case. Splitting "count" from "decide" is a useful pattern whenever a decision depends on both an aggregate (total count) and a position (who came first): compute the aggregate fully, then make the position-aware decision in a second pass.
def most_frequent_char(s):
freq = {}
for ch in s:
freq[ch] = freq.get(ch, 0) + 1
best = ""
best_count = 0
for ch in s:
if freq[ch] > best_count:
best_count = freq[ch]
best = ch
return best
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.