From cb1011783f2c7ff189322cba6c902ba46bd12c5d Mon Sep 17 00:00:00 2001 From: matheusjardimgarcia Date: Wed, 2 Nov 2022 10:54:53 -0300 Subject: [PATCH] Desafio 9 Feito --- 09-mostRepeatedChar/index.js | 38 ++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) 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