Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
O(n) solution in Clear category for The Most Wanted Letter by Sim0000
import assert from "assert";
function mostWanted(text: string): string {
let count = {};
let maxChar = '';
let maxCount = 0;
for(const c of text.toLowerCase().replace(/[^a-z]/g, '')){
count[c] = count[c] ? count[c] + 1 : 1;
if(count[c] > maxCount || count[c] == maxCount && c < maxChar){
maxCount = count[c];
maxChar = c;
}
}
return maxChar;
}
console.log('Example:');
console.log(mostWanted('Hello World!'));
// These "asserts" are used for self-checking
assert.equal(mostWanted('Hello World!'), 'l');
assert.equal(mostWanted('How do you do?'), 'o');
assert.equal(mostWanted('One'), 'e');
assert.equal(mostWanted('Oops!'), 'o');
assert.equal(mostWanted('AAaooo!!!!'), 'a');
assert.equal(mostWanted('abe'), 'a');
console.log("Coding complete? Click 'Check' to earn cool rewards!");
May 22, 2020