Skip to content

Instantly share code, notes, and snippets.

@creativemind1
Created May 22, 2026 10:12
Show Gist options
  • Select an option

  • Save creativemind1/eca062adc4911e9ced60805f18418474 to your computer and use it in GitHub Desktop.

Select an option

Save creativemind1/eca062adc4911e9ced60805f18418474 to your computer and use it in GitHub Desktop.
Count Frequency of Each Character in a String
function countLetters(str) {
const smallString = str.toLowerCase();
const obj = {};
for (let i = 0; i < smallString.length; i++) {
if (obj[smallString[i]]) {
obj[smallString[i]] += 1;
} else {
obj[smallString[i]] = 1;
}
}
return obj;
}
console.log(countLetters("My Name is Mohammed Shoeb")); //{ : 4, a: 2, b: 1, d: 1, e: 3, h: 2, i: 1, m: 5, n: 1, o: 2, s: 2, y: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment