mostRepeatedChar

This commit is contained in:
Maria Carolina Oliveira da Silva 2022-11-02 20:26:52 -03:00
parent b2ddc722a1
commit dc3e6b1151

View File

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