From c7a757284144d1ca30796ea30a368c4f49a9d28c Mon Sep 17 00:00:00 2001 From: Rafael Sampaio Date: Fri, 28 Oct 2022 10:59:45 -0300 Subject: [PATCH] feat: solucao mostRepeatedChar --- 09-mostRepeatedChar/index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..14b4a30 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,17 @@ export function mostUsedChar(text) { - // implementar logica aqui - return "" -} \ No newline at end of file + // implementar logica aqui + let arr = text.toLowerCase().split("").sort(); + let mostRepeated = null; + let moreOccurrences = -1; + + let count = 1; + for (let i = 1; i <= arr.length; i++) { + if (i < arr.length && arr[i] === arr[i - count]) { + count++; + } else if (count > moreOccurrences) { + mostRepeated = arr[i - 1]; + moreOccurrences = count; + } + } + return `${mostRepeated}`; +}