String with the highest frequency of recurring letters in a word

Viewed 1764

This is a challenge for coderbyte I thought I'd try to do it using a different method for solving it than loops, objects. It passed but it isn't perfect. The directions for the challenge are:

Have the function LetterCountI(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters return -1. Words will be separated by spaces.

 function LetterCountI(str){
  var wordsAndLetters = {};   
  var count = 0;
  var finalword;
  str = str.split(" ");
  for(var i = 0; i < str.length; i++){
    wordsAndLetters[str[i]] = wordsAndLetters[str[i]] || 0;
  }
  function countWordLetters(strs){
    strs = strs.split("");
    var lettercount = {};
    for(var i = 0; i <strs.length; i++){
      lettercount[strs[i]] = lettercount[strs[i]] || 0;
      lettercount[strs[i]]++;
    }
    return lettercount;
  }
  for(var words in wordsAndLetters){
    wordsAndLetters[words] = countWordLetters(words);
    var highestLetterFrequency = wordsAndLetters[words];
    for(var values in highestLetterFrequency){
      if(highestLetterFrequency[values] > count){
        count = highestLetterFrequency[values];
        finalword = words;
      }
      if(count !== 1){
        return finalword;
      }
    }
  }
  return -1;
}
LetterCountI("today is the greatest day ever!");

Sorry if some of the variable names are confusing I've been up for far too long trying to figure out what I did wrong. If you use the parameters at the bottom of the code it returns 'greatest' like it should however change the parameters to

LetterCountI("toddday is the greatttttest day ever!");

and it logs 'toddday' when it should log 'greatttttest'. Is my code completely wrong? I realize if the parameters were ("caatt dooog") it should log 'caatt' since there are 4 recurring letters but I'm not worried about that I just am concerned about it finding the most recurrence of one letter(but by all means if you have a solution I would like to hear it!). Any changes to the variables if needed to make this code more readable would be appreciated!

3 Answers

The problem with your code is the positioning of the following section of code:

if(count !== 1){
    return finalword;
}

Move it from where it currently is to just before the return -1, like so:

for(var words in wordsAndLetters){
    wordsAndLetters[words] = countWordLetters(words);
    var highestLetterFrequency = wordsAndLetters[words];
    for(var values in highestLetterFrequency){
      if(highestLetterFrequency[values] > count){
        count = highestLetterFrequency[values];
        finalword = words;
      }
    }
}
if(count !== 1){
    return finalword;
}
return -1;

The problem with your original code is that your were returning the first word that had repeating characters, which meant your code didn't get far enough to check if any subsequent words had more repeating characters.

Also, just for fun, here is my alternative solution.

Here you go

Array.prototype.getUnique = function(){
   var u = {}, a = [];
   for(var i = 0, l = this.length; i < l; ++i){
      if(u.hasOwnProperty(this[i])) {
         continue;
      }
      a.push(this[i]);
      u[this[i]] = 1;
   }
   return a;
}

function LetterCountI(str){
    var temp = str.split(" ");
    var final = '', weight = 0;
    for(var i = 0; i < temp.length; ++i) {
        var word = temp[i].split("");
        if(word.getUnique().length < word.length) {
            var diff = word.length - word.getUnique().length;
            if(diff > weight){
                weight = diff;
                final = temp[i];
            }
        }
    }
    return final;
}

console.log(LetterCountI("Catt dooog"));
console.log(LetterCountI("toddday is the greatttttest day ever!"));

Viva LinQ !!!!!

    var resultPerWord = new Dictionary<string, int>();

    var S = "toddday is the greatttttest day ever!";

    foreach(var s in S.Split(' ')) 
    {
        var theArray =
            from w in s
            group w by w into g
            orderby g.Count() descending
            select new { Letter = g.Key, Occurrence = g.Count() };

        resultPerWord.Add(s, theArray.First().Occurrence);
    }

    var r = "-1";
    if (resultPerWord.Any(x => x.Value >1))
    {
        r = resultPerWord.OrderByDescending(x => x.Value).First().Key;
    }
Related