Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 6, 2019 04:08
Show Gist options
  • Save masautt/2e8d8d84fd8761889ed0fc9399b4d219 to your computer and use it in GitHub Desktop.
Save masautt/2e8d8d84fd8761889ed0fc9399b4d219 to your computer and use it in GitHub Desktop.
How to find most occuring character in JavaScript
function maxChar(myStr) {
let charObj = {};
return [...myStr].reduce((_, char) => {
if (char in charObj) charObj[char]++;
else if (char !== " ") charObj[char] = 1;
return Object.keys(charObj).reduce((a, b) => {
return charObj[a] > charObj[b] ? a : b;
});
});
}
console.log(maxChar("The most reocurring character is r")) // --> "r"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment