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

18 lines
482 B
JavaScript
Raw Permalink Normal View History

export function mostUsedChar(text) {
// implementar logica aqui
2022-10-29 07:43:39 +00:00
const tl = text.length;
const freq = {};
let maxFreq = 0;
let maxChar;
for (let i = 0; i < tl; ++i){
const isPair = (text.charCodeAt(i) & 0xF800) == 0xD800;
const c = isPair ? text.substr(i++, 2) : text[i];
const f = (freq[c] || 0) + 1;
freq[c] = f;
if (f > maxFreq){
maxFreq = f;
maxChar = c;
}
}
return maxChar
}