Skip to content

Instantly share code, notes, and snippets.

@iamsaief
Last active August 20, 2021 08:49
Show Gist options
  • Save iamsaief/b7ee5576365f7b8488c3e5d41593398b to your computer and use it in GitHub Desktop.
Save iamsaief/b7ee5576365f7b8488c3e5d41593398b to your computer and use it in GitHub Desktop.
Returns character frequencies of given string
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