challenge-algorithms-v2.0/09-mostRepeatedChar/index.js

18 lines
508 B
JavaScript
Raw Normal View History

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