challenge-algorithms-v2.0/09-mostRepeatedChar/index.js
2022-11-02 20:26:52 -03:00

18 lines
508 B
JavaScript

export function mostUsedChar(text) {
const total = text.split("");
let repeat = {};
total.forEach((count) => {
repeat[count] = (repeat[count] || 0) + 1;
});
const maxVal = Math.max(...Object.values(repeat));
const num = Object.keys(repeat).find((key) => repeat[key] === maxVal);
return num;
}
console.log(mostUsedChar("fdgdfgff")); //("f")
console.log(mostUsedChar("Lorem ipsum")); //("m")
console.log(mostUsedChar("adsassdasd")); //("s")
console.log(mostUsedChar("testeeeee")); //("e")