String with the highest frequency of repeated letters in the word

This is a challenge for coderbyte. I thought that I would try to do this using a different method to solve it than loops, objects. It has passed, but it is not perfect. Directions for calling:

Let the LetterCountI (str) function accept the passed str parameter and return the first word with the most repeated letters. For example: "Today is the greatest day!" must return more because he has 2 e (and 2 t), and he comes to the point that he also has 2 e. If there are no words with duplicate 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!");

, , , . , "",

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

"toddday", "greatttttest". ? , ( "caatt dooog" ), "caatt", , , , ( , !). , , , !

+4
3

- :

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

, , return -1, :

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;

, , , , , , .

, .

+2

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!"));
0

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;
    }
0
source

All Articles