Created
December 1, 2023 00:39
-
-
Save ChathuraGH/17ef637b7c7e979c603a4fe0dc0cf35a to your computer and use it in GitHub Desktop.
Character Occurrences Counter of a 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
let s = 'hello'; | |
var result = [...s].reduce((a, e) => { a[e] = a[e] ? a[e] + 1 : 1; return a }, {}); | |
console.log(result); // {h: 1, e: 1, l: 2, o: 1} | |
//source | |
// https://stackoverflow.com/questions/19480916/count-number-of-occurrences-for-each-char-in-a-string | |
// https://stackoverflow.com/a/55540351/13861187 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment