Created
May 22, 2026 10:12
-
-
Save creativemind1/eca062adc4911e9ced60805f18418474 to your computer and use it in GitHub Desktop.
Count Frequency of Each Character in a String
This file contains hidden or 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
| 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