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

24 lines
511 B
JavaScript
Raw Permalink Normal View History

export function mostUsedChar(text) {
text.toLowerCase();
let sorted = text.split('').sort().join('');
let cont = 1;
let repeatedTimes = 0;
let mostUsed = "";
for(let i = 0; i < sorted.length; i++){
if(sorted[i] === sorted[i + 1]){
cont++;
}
else{
if(cont > repeatedTimes){
repeatedTimes = cont;
mostUsed = sorted[i];
}
cont = 1;
}
}
return mostUsed;
}