Count number of same characters in a string and display in the array using javascript

Count number of same characters in a string and display in the array using javascript

function countChars(str) {
  return str.toLowerCase().split('').reduce((total, letter) => {
    total[letter] ? total[letter]++ : total[letter] = 1;
    return total;
  }, {});

}
console.log(countChars("Australia")); 
//{ a: 3, u: 1, s: 1, t: 1, r: 1, l: 1, i: 1 }

Leave a Comment

Your email address will not be published. Required fields are marked *