Created
December 12, 2018 22:32
-
-
Save Luke-Rogerson/90f250352a6c0ec35d825ef0e06d2b25 to your computer and use it in GitHub Desktop.
Given a string, return the character that is most commonly used in the string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a string, return the character that is most | |
// commonly used in the string. | |
// --- Examples | |
// maxChar("abcccccccd") === "c" | |
// maxChar("apple 1231111") === "1" | |
function maxChar(str) { | |
const charMap = {}; | |
for (let char of str) { | |
charMap[char] = charMap[char] + 1 || 1; | |
}; | |
return Object.keys(charMap).reduce((a,b) => charMap[a] > charMap[b] ? a : b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment