Created
September 6, 2019 04:08
-
-
Save masautt/2e8d8d84fd8761889ed0fc9399b4d219 to your computer and use it in GitHub Desktop.
How to find most occuring character in JavaScript
This file contains hidden or 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
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