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

16 lines
398 B
JavaScript

export function mostUsedChar(text) {
// implementar logica aqui
const textObj = {}
let maxCount = 0
let mostUsedChar = ""
for (let char of text) {
textObj[char] = textObj[char] + 1 || 1
}
for (let key in textObj) {
if (textObj[key] > maxCount) {
maxCount = textObj[key]
mostUsedChar = key
}
}
return mostUsedChar
}