Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
function returnFirstRepeatChar2(str){
   return ((str = str.split(' ').map(function(word){
     var letters = word.split('').reduce(function(map, letter){
       map[letter] = map.hasOwnProperty(letter) ? map[letter] + 1 : 1;
       return map;
     }, {}); // map of letter to number of occurrence in the word.
     
     return {
       word: word,
       count: Object.keys(letters).filter(function(letter){
         return letters[letter] > 1;
       }).length // number of repeated letters
     };
   }).sort(function(a, b){
     return b.count - a.count;
   }).shift()) && str.count && str.word) || -1; //return first word with maximum repeated letters or -1
}
console.log(returnFirstRepeatChar2("Hello and hello again"));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers