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

17 lines
577 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("");
let timesRepeat = {};
total.forEach((count) => {
timesRepeat[count] = (timesRepeat[count] || 0) + 1;
})
const maxVal = Math.max(...Object.values(timesRepeat));
const num = Object.keys(timesRepeat).find((key) => timesRepeat[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")