mostRepeatedCh

This commit is contained in:
Maria Carolina Oliveira da Silva 2022-11-02 17:53:44 -03:00
parent b4b48956cd
commit dceca730b9

View File

@ -1,4 +1,17 @@
export function mostUsedChar(text) { export function mostUsedChar(text) {
// implementar logica aqui // implementar logica aqui
return "" 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")