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"