From e8427b22ee950ea4d4cab03fb4dafcdb82ed84e0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 1 Nov 2022 19:14:00 -0300 Subject: [PATCH] feat: criando o codigo do repeated caracterias --- 09-mostRepeatedChar/index.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..8b57af2 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,21 @@ export function mostUsedChar(text) { - // implementar logica aqui - return "" -} \ No newline at end of file + // implementar logica aqui + const stringToArray = text.split('') + const obj = {} + + stringToArray.forEach(element => { + obj[element] = obj[element] ? obj[element] + 1 : 1 + }) + + const objToArray = Object.entries(obj) + + let res = ['', 0] + + objToArray.forEach(word => { + if (res[1] < word[1]) { + res = word + } + }) + + return res[0] +}