Skip to content

Instantly share code, notes, and snippets.

@ChathuraGH
Created December 1, 2023 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChathuraGH/3e1af2e0430440dd2afb6997cdda8a4f to your computer and use it in GitHub Desktop.
Save ChathuraGH/3e1af2e0430440dd2afb6997cdda8a4f to your computer and use it in GitHub Desktop.
function countChar(str) {
let myObj= {};
for (let s of str) {
if ( myObj[s] ? myObj[s].count ++ : myObj[s] = { count : 1 } );
}
return myObj;
}
var charCount = countChar('abcceddd');
//source
// https://stackoverflow.com/questions/19480916/count-number-of-occurrences-for-each-char-in-a-string
// https://stackoverflow.com/a/67044262/13861187
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment