Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining whether an array contains duplicate values

I would like to scan through a JS array and determine if all the elements are unique, or whether the array contains duplicates.

example :

my_array1 = [1, 2, 3] 
my_array2 = [1, 1, 1]

I want get result like this :

my_array1 must be return true, because this array element is unique
and array2 must be return false, because this array element is not unique

How can I go about writing this method?

like image 593
tardjo Avatar asked Aug 18 '14 04:08

tardjo


People also ask

How do I check if an array contains unique elements?

One simple solution is to use two nested loops. For every element, check if it repeats or not. If any element repeats, return false. If no element repeats, return false.

How do you check if an array of objects has duplicate values in java?

One more way to detect duplication in the java array is adding every element of the array into HashSet which is a Set implementation. Since the add(Object obj) method of Set returns false if Set already contains an element to be added, it can be used to find out if the array contains duplicates in Java or not.

Does an array contains duplicate values C++?

Check if array contains duplicates using set The set have property to retain only distinct elements. Now we compare the size of set and size of array. If both are equal than array contains unique elements, otherwise the array contains duplicate elements.


2 Answers

Sort your array first of all, and then go for a simple comparison loop.

function checkIfArrayIsUnique(arr)  {
   var myArray = arr.sort();

    for (var i = 0; i < myArray.length; i++) {
        if (myArray.indexOf(myArray[i]) !== myArray.lastIndexOf(myArray[i])) { 
            return false; 
        } 
    } 

    return true;
}
like image 179
Manish Kr. Shukla Avatar answered Sep 27 '22 20:09

Manish Kr. Shukla


if you want to check for uniqueness you can also do this.As stated on the comment i do not assert this is as the only best option.There are some great answers down below.

var arr = [2,3,4,6,7,8,9];
var uniq = []; // we will use this to store the unique numbers found
               // in the process for doing the comparison

var result = arr.slice(0).every(function(item, index, array){
  if(uniq.indexOf(item) > -1){
    // short circuit the loop
    array.length=0; //(B)
    return false;
  }else{
    uniq.push(item);
    return true;
  }
});

result --> true

arr.slice(0) creates a temporary copy of the array, on which the actual processing is done.This is because when the uniqueness criteria is met i clear the array (B) to short circuit the loop.This will make sure the processing stops as soon as the criteria is met.

And will be more nicer if we expose this as a method on a Array instance. so we can do something like this [1,2,3,5,7].isUnique();

Add the following snippet and you are ready to go

Array.prototype.isUnique = function() {
    var uniq = [];
    var result = this.slice(0).every(function(item, index, arr) {
        if (uniq.indexOf(item) > -1) {
            arr.length = 0;
            return false;
        } else {
            uniq.push(item);
            return true;
        }
    });
    return result;
};

arr.isUnique() --> true

DEMO

like image 40
Prabhu Murthy Avatar answered Sep 27 '22 19:09

Prabhu Murthy