From 83b587aa86c05ebb96c678d4bc63e0b14e5b25fb Mon Sep 17 00:00:00 2001 From: amanda almeida Date: Fri, 28 Oct 2022 15:35:20 -0300 Subject: [PATCH] feat(mostRepeatedChar): Adiciona a logica do algoritmo que retorna a letra mais repetida de uma string --- 09-mostRepeatedChar/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..5a7730e 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,14 @@ export function mostUsedChar(text) { // implementar logica aqui - return "" + let counts = {}; + Array.from(text).forEach((count) => { + counts[count] = (counts[count] || 0) + 1; + }); + + const maxVal = Math.max(...Object.values(counts)); + const num = Object.keys(counts).find((key) => { + return counts[key] === maxVal; + }); + + return num; } \ No newline at end of file