fastest way to detect if duplicate entry exists in javascript array?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



fastest way to detect if duplicate entry exists in javascript array?


var arr = ['test0','test2','test0'];



Like the above,there are two identical entries with value "test0",how to check it most efficiently?





What criteria of efficiency? Speed? Code readability? Memory usage?
– Anatoliy
Oct 14 '09 at 7:14





do you wanna a remove it ? or just to find it ?
– ukanth
Oct 14 '09 at 7:17





Ummm... why was this question edited instead of posted as a new one? I'm reverting to the original, since now the answers make no sense.
– nickf
Oct 14 '09 at 7:30





Mask: post a new question, instead of changing this one to a completely different question.
– Miles
Oct 14 '09 at 7:41




8 Answers
8



If you sort the array, the duplicates are next to each other so that they are easy to find:


arr.sort();
var last = arr[0];
for (var i=1; i<arr.length; i++)
if (arr[i] == last) alert('Duplicate : '+last);
last = arr[i];





... assuming your values are all strings or numbers. Sorting will do no good for an arbitrary array of objects.
– Tim Down
Oct 14 '09 at 10:25





If it's an arbitrary array of objects then it's also difficult to check if they're identical.
– Skilldrick
Oct 14 '09 at 11:34





@Tim: Good point. However, if the can't be sorted, they can hardly be compared to look for duplicates...
– Guffa
Oct 14 '09 at 11:40





You can check whether any array values are identical using the identity operator (===). Sorting an array of objects to bring duplicates to the start requires knowledge of the types of objects being compared in order to decide how to compare them.
– Tim Down
Oct 14 '09 at 13:36


===



This will do the job on any array and is probably about as optimized as possible for handling the general case (finding a duplicate in any possible array). For more specific cases (e.g. arrays containing only strings) you could do better than this.


function hasDuplicate(arr)
var i = arr.length, j, val;

while (i--)
val = arr[i];
j = i;
while (j--)
if (arr[j] === val)
return true;



return false;





Thank you for this function!
– Charlie
Jun 29 '12 at 0:34



Loop stops when found first duplicate:


function has_duplicates(arr)

var x = , len = arr.length;
for (var i = 0; i < len; i++)
if (x[arr[i]])
return true;

x[arr[i]] = true;

return false;




Edit (fix 'toString' issue):


function has_duplicates(arr)

var x = , len = arr.length;
for (var i = 0; i < len; i++)
if (x[arr[i]] === true)
return true;

x[arr[i]] = true;

return false;




this will correct for case has_duplicates(['toString']); etc..





Careful: using Object as a map has difficulties. Keys may only be strings, and names that are members of Object will confuse it. eg. has_duplicates(['toString']) is true.
– bobince
Oct 14 '09 at 7:53


has_duplicates(['toString'])


true





Thanks for this. Fixed.
– Anatoliy
Oct 14 '09 at 8:45





As bobince says. All keys will be converted to strings. So the above would give a false positive with the following: [ a: 1, b: 2 ] since both members of the array will become "[object Object]" when being stored as keys in x.
– Tim Down
Oct 14 '09 at 8:53


[ a: 1, b: 2 ]


x





Gah, I hate SO on this. I downvoted and commented on your original answer and now cannot remove it.
– Tim Down
Oct 14 '09 at 8:55





In original question array contains only strings. You are correct -- my solition is not working for all possible types, but there is other task.
– Anatoliy
Oct 14 '09 at 9:12



There are lots of answers, here but not all of them "feel" nice...



If you are using lodash:


function containsDuplicates(array)
return _.uniq(array).length !== array.length;



If you can use ES6 Sets, it simply becomes:


function containsDuplicates(array)
return array.length !== new Set(array).length



With vanilla javascript:


function containsDuplicates(array)
return array
.sort()
.some(function (item, i, items)
return item === items[i + 1]
)



However, sometimes you may want to check if the items are duplicated on a certain field.



This is how I'd handle that:


containsDuplicates([country: 'AU', country: 'UK', country: 'AU'], 'country')

function containsDuplicates(array, attribute)
return array
.map(function (item) return item[attribute] )
.sort()
.some(function (item, i, items)
return item === items[i + 1]
)


var index = myArray.indexOf(strElement);
if (index < 0)
myArray.push(strElement);
console.log("Added Into Array" + strElement);
else
console.log("Already Exists at " + index);





You shall add the details to why and how is your method faster for a brief explanation.
– nullpointer
May 24 '16 at 17:32



Sorting is O(n log n) and not O(n). Building a hash map is O(n). It costs more memory than an in-place sort but you asked for the "fastest." (I'm positive this can be optimized but it is optimal up to a constant factor.)


function hasDuplicate(arr)
var hash = ;
var hasDuplicate = false;
arr.forEach(function(val)
if (hash[val])
hasDuplicate = true;
return;

hash[val] = true;
);
return hasDuplicate;



You can convert the array to to a Set instance, then convert to an array and check if the length is same before and after the conversion.


Set




const hasDuplicates = (array) =>
const arr = ['test0','test2','test0'];
const set1 = new Set(array);
const uniqueArray = [...set1];

return array.length !== uniqueArray.length;
;

console.log(`Has duplicates : $hasDuplicates(['test0','test2','test0'])`);
console.log(`Has duplicates : $hasDuplicates(['test0','test2','test3'])`);



Assuming all you want is to detect how many duplicates of 'test0' are in the array. I guess an easy way to do that is to use the join method to transform the array in a string, and then use the match method.


var arr= ['test0','test2','test0'];
var str = arr.join();

console.log(str) //"test0,test2,test0"

var duplicates = str.match(/test0/g);
var duplicateNumber = duplicates.length;

console.log(duplicateNumber); //2






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

React Native Navigation and navigating to another Screen problem