Skip to content

Instantly share code, notes, and snippets.

@ChathuraGH
Created December 1, 2023 00:39
Show Gist options
  • Save ChathuraGH/17ef637b7c7e979c603a4fe0dc0cf35a to your computer and use it in GitHub Desktop.
Save ChathuraGH/17ef637b7c7e979c603a4fe0dc0cf35a to your computer and use it in GitHub Desktop.
Character Occurrences Counter of a string
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