Skip to content

Instantly share code, notes, and snippets.

@Luke-Rogerson
Created December 12, 2018 22:32
Show Gist options
  • Save Luke-Rogerson/90f250352a6c0ec35d825ef0e06d2b25 to your computer and use it in GitHub Desktop.
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.
// 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