The problem: I want to find the word in a string that has the most repeats of a single letter, each letter is independent. Does not need to be consecutive.
Currently I am jumping each character and checking, which doesn't seem very efficient. Do you have any ideas that might improve the number of processes that are required to find which word has the most repeats?
function LetterCountI(str) {
var repeatCountList = [];
var wordList = str.split(/\W/); //regular expression \W for non word characters split at.
var wordCount = wordList.length; // count the number of words
for (var i=0; i< wordCount ; i++)
{ var mostRepeat = 1; // set the default number of repeats to 1
var curWord = wordList[i]; // set the current word to the ith word from the list
for(var ii=0; ii<curWord.length ; ii++)
{ var repeatCount = 1; // set default repeat count to 1
var curChar = curWord[ii]; //set the current character to the iith
for (var iii=0; iii<curWord.length; iii++) //judge if it is the same as the iiith
{var against = curWord[iii];
if (iii!=ii) // if it is Not the same referenced postion
{if(curChar==against) // see if the string values match
{repeatCount=repeatCount+1}}} // increase counter if match against
if (repeatCount>=mostRepeat) // record repeat for the highest only
{mostRepeat=repeatCount}
}
repeatCountList = repeatCountList.concat(mostRepeat) // take the highest from each word
}
mostRepeat = 0; // set the repeats value to -
for (j=0;j<wordCount; j++) // go through the repeats count list
{ if(repeatCountList[j]>mostRepeat) // if it has more repeats than the highest So FAR
{ mostRepeat = repeatCountList[j]; // record if higher than last
var x = j;}} // record the index of the most repeat that is the new high
var ans = [];
if (mostRepeat == 1) // check if there are no repeats at all.
{ans=-1} // question want to return -1 if there are no repeats
else
{ans=wordList[x]} // display the word from the list with the most repeat characters
// code goes here
return ans;
}
Any help is appreciated.
aabbcc
wins overhelllo
? \$\endgroup\$