From 940dba1b3852337679e0e4a0d7e0a717dfde2fa4 Mon Sep 17 00:00:00 2001 From: Bernardo Waldhelm Date: Fri, 28 Oct 2022 13:55:35 -0300 Subject: [PATCH] feat(mostUsedChar): adicionando resolucao desafio 9 mostRepeatedChar --- 09-mostRepeatedChar/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..12c72d9 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,15 @@ export function mostUsedChar(text) { // implementar logica aqui - return "" + const value = text.toLocaleLowerCase().split("").sort(); + + let counts = {}; + value.forEach(function (x) { + counts[x] = (counts[x] || 0) + 1; + }); + + const maior = Object.keys(counts).sort(function (a, b) { + return counts[a] > counts[b] ? -1 : counts[b] > counts[a] ? 1 : 0; + })[0]; + + return maior; } \ No newline at end of file