JSFiddle - React, Tailwind, and code Playground
JavaScript
function LetterCountI(str) {
var result = -1,
largestCount = 1;
var words = str.split(" ");
for (var i = 0; i < words.length; i++) {
var word = words[i];
var repeat = GetWordRepeatCount(word);
if (repeat > largestCount) {
largestCount = repeat;
result = word;
}
}
return result;
}
function GetWordRepeatCount(str) {
//get each letter count for the word
var counts = {};
for (var i = 0; i < str.length; i++) {
var char = str[i];
counts[char] = (counts[char] || 0) + 1;
}
//get the largest number for the letter counts
var max = 0;
for (var c in counts) {
if (counts[c] > max) max = counts[c];
}
return max;
}
console.log(LetterCountI("Hello there"));
console.log(LetterCountI("No words"));
console.log(LetterCountI("How about these words?"));