Q: Write a function called countLetters which accepts a string and returns the values as the number of times the letter appears in the string. Hi, Is there a more efficient way to solve that question?
O(n) solution, howto make it O(log n)
function countLetters(str){
var splitArr = str.toLowerCase().split("");
var obj = {};
var letters = "abcdefghijklmnopqrstuvwxez";
splitArr.forEach((letter)=>{
if(letters.includes(letter)){
if(letter in obj){
obj[letter]++;
} else{
obj[letter] = 1;
}
}
});
return obj;
}
console.log(countLetters('Ellie'))