Skip to content

Instantly share code, notes, and snippets.

@pablodenadai
Last active February 8, 2017 05:31
Show Gist options
  • Save pablodenadai/80dbc916d11bd39a134239c97c87ad21 to your computer and use it in GitHub Desktop.
Save pablodenadai/80dbc916d11bd39a134239c97c87ad21 to your computer and use it in GitHub Desktop.
Calculate the mode of a given string - JavaScript ES6
const mode = (str) => {
return str
.split(' ')
.reduce((data, key) => {
let counter = data.map[key] + 1 || 1
data.map[key] = counter
if (counter > data.counter) {
data.counter = counter
data.mode = key
}
return data
}, {
counter: 0,
mode: null,
map: {}
})
.mode
}
console.log(mode('the t-rex is the greatest of them all'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment