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

17 lines
547 B
JavaScript
Raw Normal View History

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