challenge-algorithms-v2.0-S.../09-mostRepeatedChar/index.js

29 lines
759 B
JavaScript

export function mostUsedChar(text) {
let vetChar = [];
let qtdCharAtual = 0;
let charAtual = "";
let qtdCharMaior = 0;
let charMaior = "";
for (let index = 0; index < text.length; index++) {
const element = text[index];
if (!vetChar.includes(element)) {
vetChar.push(element);
}
}
for (let chave = 0; chave <= vetChar.length; chave++) {
charAtual = vetChar[chave];
qtdCharAtual = 0;
for (let index = 0; index <= text.length; index++) {
if (vetChar[chave] === text[index]) {
qtdCharAtual = qtdCharAtual + 1;
}
if (qtdCharMaior < qtdCharAtual && vetChar[chave] !== text[index]) {
charMaior = charAtual;
qtdCharMaior = qtdCharAtual;
}
}
}
return charMaior;
}