Return the character has the highest frequency in a string.
Anonymous
function mostRepeatedString(s) { const occurances = new Array(128).fill(0); let maxChar = ''; let max = -1; s.split("").forEach((c) => { const code = c.charCodeAt(0); occurances[code]++; if (occurances[code] > max) { max = occurances[code]; maxChar = c; } }); return maxChar; } // Time: O(n), space O(1);
Check out your Company Bowl for anonymous work chats.