From 7ec610d9c9c9b0c86d4a17b6d9c687e365d9da8b Mon Sep 17 00:00:00 2001 From: Sabrina Miranda Date: Fri, 28 Oct 2022 19:44:32 -0300 Subject: [PATCH] feat: Implementa Desafio 2.9 --- 09-mostRepeatedChar/index.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..c9f7abc 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,27 @@ export function mostUsedChar(text) { - // implementar logica aqui - return "" + let texto = text.toLowerCase().replace(/ /g, "").split("").sort(); + let letras = []; + let contadorLetras = 0; + let letrasRepetidas = []; + + for(let i = 0 ; i < texto.length ; i++) { + if(texto[i + 1] === texto[i]) { + contadorLetras++; + }else { + letras.push(texto[i]); + letrasRepetidas.push(contadorLetras); + contadorLetras = 1; + } + } + + let valorMaximoLetras = Math.max(...letrasRepetidas); + let letraMaisRepetida = ""; + + for(let i = 0 ; i < letrasRepetidas.length ; i++) { + if(letrasRepetidas[i] == valorMaximoLetras) { + letraMaisRepetida = letras[i]; + } + } + + return letraMaisRepetida; } \ No newline at end of file