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?
-
2You should edit your question to show what you've done so far, and where you're having trouble.David Makogon– David Makogon2017-03-19 04:33:46 +00:00Commented Mar 19, 2017 at 4:33
-
1There are any number of existing questions about counting duplicates in an array. Surely you can adapt the code from one or another of them? Have a look at the "Related" questions listed on the bottom-right of this page.nnnnnn– nnnnnn2017-03-19 04:38:20 +00:00Commented Mar 19, 2017 at 4:38
5 Answers
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;
})
)
Comments
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");
}
});
Comments
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']);
2 Comments
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.
Comments
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);