-1

I have an array with several names like : [mike,bob,john,john,rick,bob] Can someone please tell me how is the most efficient way to find which name has been repeated the most?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
JohnSnow
  • 6,911
  • 8
  • 23
  • 44
  • 2
    You should edit your question to show what you've done so far, and where you're having trouble. – David Makogon Mar 19 '17 at 04:33
  • 1
    There are any number of existing questions about counting duplicates in an array. Surely you can adapt the code from [one](http://stackoverflow.com/questions/26237031/check-an-array-for-duplicate-values-get-value-and-get-count?noredirect=1&lq=1) or [another](http://stackoverflow.com/questions/12749200/how-to-count-array-elements-by-each-element-in-javascript) of them? Have a look at the "Related" questions listed on the bottom-right of this page. – nnnnnn Mar 19 '17 at 04:38

5 Answers5

0

Generate an object with count using Array#reduce method, later sort the property name array(get using Object.keys method) based on the count using Array#sort method and finally get the first element.

var arr = ['mike', 'bob', 'john', 'john', 'rick', 'bob'];

// generate an object which holds the count
var obj = arr.reduce(function(o, v) {
  // define the property if not defined
  o[v] = o[v] || 0;
  // increment the count
  o[v]++;
  // return the object reference
  return o;
  // set initial value as an empty object
}, {});

// sort based on the count, descending order
// and get first
console.log(
  Object.keys(obj).sort(function(a, b) {
    return obj[b] - obj[a];
  })[0]
)

// or using reduce, reduce into a single value 
// which holds the highest value
console.log(
  Object.keys(obj).reduce(function(a, b) {
    return obj[b] > obj[a] ? b : a;
  })
)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

The standard solution to find duplicates (in O(n) time) is to walk linearly through the list and add each item to a set. Each time, before doing so, check if it is already in the set. If it is, you've found a duplicate.

names = [ 'mike', 'bob', 'john', 'john', 'rick', 'bob'];

seen = new Set();

names.forEach(function(item, index, array) {
    if (seen.has(item)) {
        console.log(item + " is a duplicate");
    } else {
        seen.add(item);
    }
});

Alternately, you can sort in O(n log(n)) time, and save the extra space that was used above, by sorting and checking pair-wise as you iterate over the array:

names = [ 'mike', 'bob', 'john', 'john', 'rick', 'bob'];

names.sort().forEach(function(item, index, array) {
    if ((index > 0) && (array[index - 1] == item)) {
        console.log(item + " is a duplicate");
    }
});
Jameson
  • 6,400
  • 6
  • 32
  • 53
0

You could find with the help of map as follows

   function findDuplicates() {
     var name, names = ['mike', 'bob', 'john', 'john', 'rick', 'bob'];
     var map = new Map();
     var max = 1;
     var maxRecurringString = "";
        for(name of names) {
    
           if(map.get(name) === undefined) {
              map.set(name, 1);
           } else {
              var count = map.get(name);
              count = count+1;
              map.set(name, count);
                  if(max < count) {
                     max = count;
                     maxRecurringString = name;
                  }
            }
     }
    console.log("Maximum recurring string is ", maxRecurringString, ". Max number of times :" + max);
  }
findDuplicates();

This snippet prints the first string which appears maximum number of times. Say for example in the above example bob and john has appeared twice. If you want all the strings to be printed which has appeared maximum number of times you can iterate for the map whose count is the max count to print all the strings.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
0

function findDup(arr){
   var obj={};
   for(var i of arr){
      if(obj[i]==1){
        console.log(i);
      }
     obj[i]=1;
   }
 }
 findDup([ 'mike', 'bob', 'john', 'john', 'rick', 'bob']);
0

If you're looking for the most efficient way to do this, talking about performance, the best way is to loop over the array only once.

Array#reduce, Array#sort, Array#forEach will loop over the entire array, so if you're concerned about performance (specially working with considerable amount of data), avoiding those is the best practice

var findHighestRecurring = function(arr) {
    arr.sort();

    var i        = arr.length;
    var max      = { item:"", count: 0 };
    var last     = { item:"", count: 0 };
    var validate = function() {
        if (last.count > max.count) {
            max.item  = last.item;
            max.count = last.count;
        }
    }

    while (i--) {
        var curr = arr[i];

        if (last.item !== curr) {
            validate();
            
            last.item  = curr;
            last.count = 0;
        }
        
        last.count++;
    }

    validate();

    return max;
}

var sample = ["mike","bob","john","bob","john","rick","bob"];
var result = findHighestRecurring(sample);

console.log(result);
Santiago Hernández
  • 5,438
  • 2
  • 26
  • 34