I’m trying to build a function that takes a string as a parameter and return the character or characters that appear the most in the that string.
I have tried the code below but it only returns an array with the keys. I want to return someone like that: e appears 4 times and b appears 4 times in case both of them have the max value.
Below is my code and how I started:
const frequentChar = str =>{
let count = {}
for(let char of str.replace(/s/g, '')){
if(count[char]){
count[char] +=1
}else{
count[char] = 1
}
}
const result = Object.keys(count).filter(x => {
return count[x] == Math.max.apply(null, Object.values(count));
});
console.log(result);
}
frequentChar(text)