Skip to main content

[Easy] Find Most Frequent Character

1. Question Description

Question Description

Implement a function that takes a string and returns the character that appears most frequently.

Examples

findMostFrequentChar('abcccccccd'); // 'c'
findMostFrequentChar('hello world'); // 'l'
findMostFrequentChar('javascript'); // 'a'

2. Implementation Methods

Implementation Methods

Method 1: Object Counting (Basic)

Approach: Iterate through the string, use an object to record each character's count, then find the character with the highest count.

function findMostFrequentChar(str) {
// Initialize object to store characters and counts
const charCount = {};

// Initialize variables for tracking max count and character
let maxCount = 0;
let maxChar = '';

// Iterate through the string
for (let char of str) {
// If the character is not in the object, set count to 0
if (!charCount[char]) {
charCount[char] = 0;
}

// Increment this character's count
charCount[char]++;

// If this character's count is greater than the max count,
// update the max count and max character
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

// Return the most frequent character
return maxChar;
}

// Test
console.log(findMostFrequentChar('abcccccccd')); // 'c'
console.log(findMostFrequentChar('hello world')); // 'l'

Time Complexity: O(n), where n is the string length Space Complexity: O(k), where k is the number of distinct characters

Method 2: Count First, Then Find Max (Two-phase)

Approach: First iterate once to count all characters, then iterate again to find the maximum.

function findMostFrequentChar(str) {
// Phase 1: Count occurrences of each character
const charCount = {};
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}

// Phase 2: Find the character with the most occurrences
let maxCount = 0;
let maxChar = '';

for (let char in charCount) {
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

return maxChar;
}

// Test
console.log(findMostFrequentChar('abcccccccd')); // 'c'

Pros: Clearer logic, phased processing Cons: Requires two iterations

Method 3: Using Map (ES6)

Approach: Use a Map to store the mapping between characters and their counts.

function findMostFrequentChar(str) {
const charCount = new Map();
let maxCount = 0;
let maxChar = '';

for (let char of str) {
const count = (charCount.get(char) || 0) + 1;
charCount.set(char, count);

if (count > maxCount) {
maxCount = count;
maxChar = char;
}
}

return maxChar;
}

// Test
console.log(findMostFrequentChar('abcccccccd')); // 'c'

Pros: Using Map is more idiomatic in modern JavaScript Cons: For simple cases, a plain object may be more intuitive

Method 4: Using reduce (Functional Style)

Approach: Use reduce and Object.entries for implementation.

function findMostFrequentChar(str) {
// Count occurrences of each character
const charCount = str.split('').reduce((acc, char) => {
acc[char] = (acc[char] || 0) + 1;
return acc;
}, {});

// Find the character with the most occurrences
return Object.entries(charCount).reduce((max, [char, count]) => {
return count > max[1] ? [char, count] : max;
}, ['', 0])[0];
}

// Test
console.log(findMostFrequentChar('abcccccccd')); // 'c'

Pros: Functional style, concise code Cons: Less readable, slightly lower performance

Method 5: Handling Multiple Characters with the Same Max Count

Approach: If multiple characters have the same highest count, return an array or the first one encountered.

function findMostFrequentChar(str) {
const charCount = {};
let maxCount = 0;

// Count occurrences of each character
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
maxCount = Math.max(maxCount, charCount[char]);
}

// Find all characters with the max count
const mostFrequentChars = [];
for (let char in charCount) {
if (charCount[char] === maxCount) {
mostFrequentChars.push(char);
}
}

// Return the first encountered (or return the entire array)
return mostFrequentChars[0];
// Or return all: return mostFrequentChars;
}

// Test
console.log(findMostFrequentChar('aabbcc')); // 'a' (first encountered)

3. Edge Cases

Edge Case Handling

Handling Empty Strings

function findMostFrequentChar(str) {
if (!str || str.length === 0) {
return ''; // Or throw new Error('String cannot be empty')
}

const charCount = {};
let maxCount = 0;
let maxChar = '';

for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

return maxChar;
}

Handling Case Sensitivity

function findMostFrequentChar(str, caseSensitive = true) {
const processedStr = caseSensitive ? str : str.toLowerCase();
const charCount = {};
let maxCount = 0;
let maxChar = '';

for (let char of processedStr) {
charCount[char] = (charCount[char] || 0) + 1;
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

return maxChar;
}

// Test
console.log(findMostFrequentChar('Hello', false)); // 'l' (case-insensitive)
console.log(findMostFrequentChar('Hello', true)); // 'l' (case-sensitive)

Handling Spaces and Special Characters

function findMostFrequentChar(str, ignoreSpaces = false) {
const processedStr = ignoreSpaces ? str.replace(/\s/g, '') : str;
const charCount = {};
let maxCount = 0;
let maxChar = '';

for (let char of processedStr) {
charCount[char] = (charCount[char] || 0) + 1;
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

return maxChar;
}

// Test
console.log(findMostFrequentChar('hello world', true)); // 'l' (ignoring spaces)
console.log(findMostFrequentChar('hello world', false)); // ' ' (space)

4. Common Interview Questions

Common Interview Questions

Question 1: Basic Implementation

Implement a function that finds the most frequent character in a string.

Click to reveal answer
function findMostFrequentChar(str) {
const charCount = {};
let maxCount = 0;
let maxChar = '';

for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

return maxChar;
}

// Test
console.log(findMostFrequentChar('abcccccccd')); // 'c'
console.log(findMostFrequentChar('hello world')); // 'l'

Key points:

  • Use an object or Map to record each character's count
  • Update the maximum during iteration
  • Time complexity O(n), space complexity O(k)

Question 2: Optimized Version

Optimize the above function to handle multiple characters with the same max count.

Click to reveal answer
function findMostFrequentChar(str) {
const charCount = {};
let maxCount = 0;

// Phase 1: Count occurrences of each character
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
maxCount = Math.max(maxCount, charCount[char]);
}

// Phase 2: Find all characters with the max count
const mostFrequentChars = [];
for (let char in charCount) {
if (charCount[char] === maxCount) {
mostFrequentChars.push(char);
}
}

// Return first or all based on requirements
return mostFrequentChars[0]; // Or return mostFrequentChars
}

// Test
console.log(findMostFrequentChar('aabbcc')); // 'a'

Question 3: Using Map

Implement this function using ES6 Map.

Click to reveal answer
function findMostFrequentChar(str) {
const charCount = new Map();
let maxCount = 0;
let maxChar = '';

for (let char of str) {
const count = (charCount.get(char) || 0) + 1;
charCount.set(char, count);

if (count > maxCount) {
maxCount = count;
maxChar = char;
}
}

return maxChar;
}

// Test
console.log(findMostFrequentChar('abcccccccd')); // 'c'

Map vs Object:

  • Map: Better suited for dynamic key-value pairs; keys can be any type
  • Object: Simpler and more intuitive; suitable for string keys

5. Best Practices

Best Practices

// 1. Use clear variable names
function findMostFrequentChar(str) {
const charCount = {}; // Clearly expresses purpose
let maxCount = 0;
let maxChar = '';
// ...
}

// 2. Handle edge cases
function findMostFrequentChar(str) {
if (!str || str.length === 0) {
return '';
}
// ...
}

// 3. Update max during iteration (single pass)
function findMostFrequentChar(str) {
const charCount = {};
let maxCount = 0;
let maxChar = '';

for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}

return maxChar;
}

Approaches to Avoid

// 1. Don't use two iterations (unless necessary)
// ❌ Lower performance
function findMostFrequentChar(str) {
const charCount = {};
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}
// Second iteration
return Object.entries(charCount).sort((a, b) => b[1] - a[1])[0][0];
}

// 2. Don't forget to handle empty strings
// ❌ May return undefined
function findMostFrequentChar(str) {
const charCount = {};
// ...
return maxChar; // maxChar is '' for empty string
}

// 3. Don't use overly complex functional patterns (unless team convention)
// ❌ Less readable
const findMostFrequentChar = (str) =>
Object.entries(
str.split('').reduce((acc, char) => {
acc[char] = (acc[char] || 0) + 1;
return acc;
}, {})
).reduce((max, [char, count]) => (count > max[1] ? [char, count] : max), ['', 0])[0];

6. Interview Summary

Interview Summary

Quick Reference

Implementation Steps:

  1. Use an object or Map to record each character's count
  2. Update the maximum during iteration
  3. Time complexity O(n), space complexity O(k)
  4. Handle edge cases (empty string, case sensitivity, etc.)

Optimization Directions:

  • Complete in a single pass (count and find max simultaneously)
  • Use Map for complex scenarios
  • Handle multiple characters with the same max count
  • Consider case sensitivity, spaces, and other special cases

Interview Answer Example

Q: Implement a function that finds the most frequent character in a string.

"I would use an object to record each character's count, then update the maximum during string iteration. Specifically: initialize an empty object charCount to store characters and their counts, and initialize maxCount and maxChar variables. Then iterate through the string — for each character, initialize to 0 if not in the object, then increment the count. If the current character's count exceeds maxCount, update maxCount and maxChar. Finally, return maxChar. This approach has O(n) time complexity and O(k) space complexity, where n is the string length and k is the number of distinct characters."

Reference