I had the following problem in a interview which I did not pass and I been trying to make it work for me and following is my solution but ATM, the number is one more than what is given as argument
function checkForMostRepeatedWord(string){
let obj = {}
if(!string){
return
}
for(let i = 0; i <string.length; i++){
obj[string[i]] = 1
for(let x = 0; x < string.length; x++){
if(string[i] == string[x]){
obj[string[i]] += 1
}
}
}
console.log(Object.keys(obj));
console.log(Object.values(obj));
return Object.values(obj)
}
checkForMostRepeatedWord("hheeell");
The following is the output
[ 'h', 'e', 'l' ]
[ 3, 4, 3 ]
Why is it printing a extra number whereas h
is only 2 times.