JavaScript Program to Find Most Frequent Character
function mostFrequentChar(str) { const counts = {}; for (const char of str) { counts[char] = (counts[char] || 0) + 1; } let maxChar = ''; let maxCount = 0; for (const char in counts) { if (counts[char] > maxCount) { maxCount = counts[char]; maxChar = char; } } return maxChar; }Examples
How to Think About It
Algorithm
Code
function mostFrequentChar(str) { const counts = {}; for (const char of str) { counts[char] = (counts[char] || 0) + 1; } let maxChar = ''; let maxCount = 0; for (const char in counts) { if (counts[char] > maxCount) { maxCount = counts[char]; maxChar = char; } } return maxChar; } console.log(mostFrequentChar('hello'));
Dry Run
Let's trace the input 'hello' through the code
Initialize counts object
counts = {}
Count characters
After looping: counts = { h: 1, e: 1, l: 2, o: 1 }
Find max character
maxChar = '', maxCount = 0 Check 'h': count=1 > 0, maxChar='h', maxCount=1 Check 'e': count=1 == maxCount, no change Check 'l': count=2 > 1, maxChar='l', maxCount=2 Check 'o': count=1 < maxCount, no change
Return result
Return 'l'
| Character | Count | MaxChar | MaxCount |
|---|---|---|---|
| h | 1 | h | 1 |
| e | 1 | h | 1 |
| l | 2 | l | 2 |
| o | 1 | l | 2 |
Why This Works
Step 1: Counting characters
The code uses an object to keep track of how many times each character appears by adding 1 each time it sees the character.
Step 2: Finding the maximum
It then checks all counts to find the character with the highest number, updating the max when it finds a bigger count.
Step 3: Returning the result
Finally, it returns the character that appeared the most times as the answer.
Alternative Approaches
function mostFrequentChar(str) { const counts = new Map(); for (const char of str) { counts.set(char, (counts.get(char) || 0) + 1); } let maxChar = ''; let maxCount = 0; for (const [char, count] of counts) { if (count > maxCount) { maxCount = count; maxChar = char; } } return maxChar; } console.log(mostFrequentChar('hello'));
function mostFrequentChar(str) { const counts = [...str].reduce((acc, char) => { acc[char] = (acc[char] || 0) + 1; return acc; }, {}); return Object.keys(counts).reduce((a, b) => counts[a] > counts[b] ? a : b); } console.log(mostFrequentChar('hello'));
Complexity: O(n) time, O(k) space
Time Complexity
The program loops through the string once to count characters, which takes O(n) time where n is string length. Then it loops through the counts object which has at most k keys (unique chars), so total is O(n + k), simplified to O(n).
Space Complexity
It uses extra space to store counts for each unique character, so space is O(k), where k is number of unique characters.
Which Approach is Fastest?
Using an object or Map both have similar performance. The reduce method is concise but not faster. The main cost is counting characters once.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Object counting | O(n) | O(k) | Simple and fast for most cases |
| Map counting | O(n) | O(k) | Better key handling, slightly more complex |
| Reduce method | O(n) | O(k) | Concise code, less beginner-friendly |