Skip to content

Instantly share code, notes, and snippets.

@violet-athena
Last active August 12, 2019 16:11
Show Gist options
  • Save violet-athena/581b499ccfe62cf960c9f56b11b4baea to your computer and use it in GitHub Desktop.
Save violet-athena/581b499ccfe62cf960c9f56b11b4baea to your computer and use it in GitHub Desktop.
Find the the most frequent character in a string
const getCharacterFrequency = string => string.split('').reduce((acc, char) => {
const { length } = string.split(char);
if (length > acc.frequency) {
acc.frequency = length;
acc.mostFrequent = char;
}
return acc;
}, { frequency: 0, mostFrequent: '' }).mostFrequent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment