Created
December 15, 2019 20:55
-
-
Save lomse/5756259b8657ea00c2835983d621c8c8 to your computer and use it in GitHub Desktop.
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(str) { | |
const arr = str.split(""); | |
// map letters to total number of occurences | |
const map = arr.reduce((acc, letter) => { | |
if (letter.trim()) { | |
acc.set(letter, (acc.get(letter) || 0) + 1) | |
} | |
return acc; | |
}, new Map()); | |
// map the letter that has higher number of occurrences to count | |
const res = arr.reduce((max, letter) => { | |
if (map.get(letter) > max.count) { | |
return { letter, count: map.get(letter) } | |
} | |
return max | |
}, { | |
letter: "", | |
count: 0 | |
}) | |
return res.letter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment