0

Ok, perhaps my last question I was not clear enough.

I want to return the word with the most repeated characters in a string.

so the following string:

There/'s a passage that I got memorized, seems appropriate for this situation: Ezekiel 25,17.

would return ["appropriate"].

But in the event that there is more than one word with the same amount of repeated characters, I would like to return them all. so the following string

Hello all from Boston

would return ["Hello", "all", "boston"]

Here is the code that I have so far. This code was taken from this other stackoverflow thread

function returnRepeatChar(str){
  var maxCount = 0;
  var word = '-1';
  
  //split string into words based on spaces and count repeated characters
  str.toLowerCase().split(" ").forEach(function(currentWord){
    var hash = {};
    
    //split word into characters and increment a hash map for repeated values
    currentWord.split('').forEach(function(letter){
      if (hash.hasOwnProperty(letter)){
        hash[letter]++;
      } else {
        hash[letter] = 1;
      }
    });
    
    //convert the hash map to an array of character counts
    var characterCounts = Object.keys(hash).map(function(key){
      return hash[key];
    });
    
    //find the maximum value in the squashed array
    var currentMaxRepeatedCount = Math.max.apply(null, characterCounts);
    
    //if the current word has a higher repeat count than previous max, replace it 
    if (currentMaxRepeatedCount > maxCount){
      maxCount = currentMaxRepeatedCount;
      word = currentWord;
    }
    
  });
  return word;
}

console.log(returnRepeatChar("There/'s a passage that I got memorized, seems appropiate for this situation: Ezekiel 25,17.")); //"appropriate"
Community
  • 1
  • 1
Lucky500
  • 677
  • 5
  • 17
  • 31
  • 1
    For "Chris's spleen", is the result `[Chris's, spleen]` or `[spleen]`? And is "man's dog" `[man's, dog]` or `[man, dog]`? – Millie Smith Feb 12 '16 at 03:39
  • Change `word` from a string to an array. If the current count is greater than the max count, set `word` to an array containing the current word. If the current count is the same as the max count, push the current word onto the array. Otherwise, it's exactly the same as what you have. – Barmar Feb 12 '16 at 03:45
  • If you ignore the difficulty of tokenizing into words, the question can be broken into small, simple steps. Build an array that holds the "score" for each token, keeping track of what the max score is. Then take all the words that have the max score. The scoring function for a word is pretty simple... it's simply a loop. – Millie Smith Feb 12 '16 at 03:45
  • I don't think I have to worry about ' , but in your scenario then, [Chris's, spleen] and man's dog there is no repeat of words, so it would return -1... – Lucky500 Feb 12 '16 at 03:45
  • By asking about "man's dog", I was trying to ask if the word is "man's" or "man". I think your first answer answers that though. Thanks. – Millie Smith Feb 12 '16 at 03:46
  • The answers given were actually correct, given I did not express myself clearly enough. That is why I posted another question. – Lucky500 Feb 12 '16 at 04:13

2 Answers2

2

Make the following simple changes to your code.

var word = "-1";

becomes:

var word = [];

The code that updates word becomes:

//if the current word has a higher repeat count than previous max, replace it 
if (currentMaxRepeatedCount > maxCount){
  maxCount = currentMaxRepeatedCount;
  word = [currentWord];
} else if (currentMaxRepeatedCount == maxCount) {
  // If it's the same as the max, add it to the list
  word.push(currentWord);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

Yes you can do it. try the following scripts.

function largest(arr){
        var sortArr = arr.sort(function(a,b){return b.length - a.length});
        return sortArr;
    }

    function returnRepeatChar(str){
       return largest(str.toLowerCase().split(" "));
    }
    //This function return the sorted of the given string
    var sorted = returnRepeatChar("There\'s a passage that I got memorized, seems appropriate for this situation: Ezekiel 25,17.");
alert(sorted);
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • Where does your `largest` function count the number of repeated characters in the word? It looks like you're just finding the longest word in the sentence. – Barmar Feb 12 '16 at 03:55
  • And you're just returning a single word, not all the words with the most repeats. – Barmar Feb 12 '16 at 03:56
  • The whole difference between this question and the previous question that he linked to is that the other question only found one word, he wants all of them. You seem to have ignored that critical detail completely. – Barmar Feb 12 '16 at 03:57
  • @Barmar, Okey, Thanks. I have to be more careful for the next time before any activity. – Murad Hasan Feb 12 '16 at 03:59
  • I don't think `return console.log(...` is going to do what you want it to. –  Feb 12 '16 at 04:05