From c04833fec3c8154fe58aaccbdd79bedab8cc59ee Mon Sep 17 00:00:00 2001 From: Luiz Felipe Silva Date: Mon, 31 Oct 2022 23:57:33 -0300 Subject: [PATCH] feat(mostUsedChar): algoritmo criado. --- 09-mostRepeatedChar/index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/09-mostRepeatedChar/index.js b/09-mostRepeatedChar/index.js index b113ed8..fe038fa 100644 --- a/09-mostRepeatedChar/index.js +++ b/09-mostRepeatedChar/index.js @@ -1,4 +1,21 @@ export function mostUsedChar(text) { // implementar logica aqui - return "" + let array = []; + let stringText = text.toLowerCase(); + let letrasJuntas = stringText.replace(/ /g, ''); + + for(let i = 0; i < letrasJuntas.length; i++) { + let letra = letrasJuntas.charAt(i); + + if(array[letra] != undefined) array[letra]++ + else array[letra] = 1; + } + + let letrasOrdenadas = Object.keys(array).sort(function order(key1, key2) { + if(array[key1] > array[key2]) return -1; + else if(array[key1] < array[key2]) return +1; + else return 0; + })[0]; + + return letrasOrdenadas; } \ No newline at end of file