From 2d3fe20e79093fe26d994009f2b022bc38f44409 Mon Sep 17 00:00:00 2001 From: Rhayllon Date: Sat, 29 Oct 2022 04:43:39 -0300 Subject: [PATCH] Feat(Desafio_2.9): Cria desafio 2.9 --- 09-mostRepeatedChar/index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..c9e5eb3 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,18 @@ export function mostUsedChar(text) { // implementar logica aqui - return "" + const tl = text.length; + const freq = {}; + let maxFreq = 0; + let maxChar; + for (let i = 0; i < tl; ++i){ + const isPair = (text.charCodeAt(i) & 0xF800) == 0xD800; + const c = isPair ? text.substr(i++, 2) : text[i]; + const f = (freq[c] || 0) + 1; + freq[c] = f; + if (f > maxFreq){ + maxFreq = f; + maxChar = c; + } + } + return maxChar } \ No newline at end of file