Skip to content

Instantly share code, notes, and snippets.

@lomse
Created December 15, 2019 20:55
Show Gist options
  • Save lomse/5756259b8657ea00c2835983d621c8c8 to your computer and use it in GitHub Desktop.
Save lomse/5756259b8657ea00c2835983d621c8c8 to your computer and use it in GitHub Desktop.
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