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

18 lines
446 B
JavaScript
Raw Normal View History

export function mostUsedChar(text) {
2022-10-28 13:59:45 +00:00
// implementar logica aqui
let arr = text.toLowerCase().split("").sort();
let mostRepeated = null;
let moreOccurrences = -1;
let count = 1;
for (let i = 1; i <= arr.length; i++) {
if (i < arr.length && arr[i] === arr[i - count]) {
count++;
} else if (count > moreOccurrences) {
mostRepeated = arr[i - 1];
moreOccurrences = count;
}
}
return `${mostRepeated}`;
}