Skip to content

Instantly share code, notes, and snippets.

@ChathuraGH
Created December 1, 2023 19:27
Show Gist options
  • Save ChathuraGH/8bbd6e132888422195a319fd1394f761 to your computer and use it in GitHub Desktop.
Save ChathuraGH/8bbd6e132888422195a319fd1394f761 to your computer and use it in GitHub Desktop.
function Char_Count(str1) {
var chars = {};
str1.replace(/\S/g, function(l){chars[l] = (isNaN(chars[l]) ? 1 : chars[l] + 1);});
return chars;
}
var myString = "This is my String";
console.log(Char_Count(myString));
//source
// https://stackoverflow.com/questions/19480916/count-number-of-occurrences-for-each-char-in-a-string
// https://stackoverflow.com/a/46297978/13861187
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment