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