Last active
August 20, 2021 08:49
-
-
Save iamsaief/b7ee5576365f7b8488c3e5d41593398b to your computer and use it in GitHub Desktop.
Returns character frequencies of given string
This file contains 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
const characterMap = (str) => { | |
const charMap = {}; | |
str.split("").forEach((char) => { | |
if (charMap[char]) { | |
charMap[char]++; | |
} else { | |
charMap[char] = 1; | |
} | |
}); | |
return charMap; | |
}; | |
console.log(characterMap("Hello")); | |
// output: { h: 1, e: 1, l: 2, o:1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment