diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..9833767 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,34 @@ -export function mostUsedChar(text) { - // implementar logica aqui - return "" -} \ No newline at end of file +export function mostUsedChar(str) { + const charMap = {}; + let max = 0; + let mostUsedChar = ''; + + // create character map + for (let char of str) { + if (charMap[char]) { + // increment the character's value if the character existed in the map + charMap[char]++; + } else { + // Otherwise, the value of the character will be increamented by 1 + charMap[char] = 1; + } + } + + // find the most commonly used character + for (let char in charMap) { + if (charMap[char] > max) { + max = charMap[char]; + mostUsedChar = char; + } + } + + return mostUsedChar; + } + + console.log(mostUsedChar("fdgdfgff")); + + console.log(mostUsedChar("Lorem ipsum")); + + console.log(mostUsedChar("adsassdasd")); + + console.log(mostUsedChar("testeeeee")); \ No newline at end of file