DEV Community

Clever Cottonmouth
Clever Cottonmouth

Posted on

Find the number of occurrences for each character in a string in javascript

  let str="hell" 
            var count = {};
            for (let i = 0; i < str.length; i++) {
                var ch = str[i];
                if (count[ch] > 0) {
                    count[ch]++;
                } else {
                    count[ch] = 1;
                }
            }
            for(let i in count){
                console.log(i,count[i])
            }

Enter fullscreen mode Exit fullscreen mode

Top comments (0)