🟢 Easy 🧩 Pattern – Strings and Loops
From the given string of Characters (can be a mix of numbers, alphabets, and other special characters) and we need to find the maximum occurring character.
Examples
Input: "Helooo"
Output: 'o'
Input: "MatrixreadM"
Output: 'M'Code language: JavaScript (javascript)JS Solution
function maxOccuringCharacter(str) {
const charCount = {};
let maxChar = '';
let maxCount = 0;
for (const char of str) {
// Increment character count
charCount[char] = (charCount[char] || 0) + 1;
// Update maxChar and maxCount if current count exceeds maxCount
if (charCount[char] > maxCount) {
maxChar = char;
maxCount = charCount[char];
}
}
return maxChar;
}
console.log(maxOccuringCharacter("hello hi how")); // 'h'Code language: JavaScript (javascript)