0

I am trying to check duplicate in the following array, just i am want to return true or false if duplicate exists.

var array = ["Saturday", "Sunday", "Monday", "Tuesday", "Saturday" ];

for ( var i = 0; i < array.length; i++){

for (var j = i+1; j< array.length; j++){

if (array [i] === array [j]){

console.log(array[i]);
}
}

}

I tried the above , it giving result only for one item in an array not for comma separated. How i can write a duplicate check function in best way for comma separated array?

Array(10)
 0: "test3,tier 1,test,test2
"1: "test3,tier 1,test,test2
"2: "test3,tier 1,test,test2
"3: "test3,tier 1,test,test2
"4: "test3,tier 1,test,test2
"5: "test3,tier 1,test,test2
"6: "test3,tier 1,test,test2
"7: "test3,tier 1,test,test2
"8: "test3,tier 1,test,test2
"9: "test3,tier 1,test,test2
"length: 10
__proto__: Array(0)
6
  • 1
    oh, so your real array has string values that have commas in them, they're just strings, the commas mean nothing special - so what do you want to test exactly? the code will check and output the duplicates (i.e. all 10 are the same) Commented Jul 31, 2018 at 9:56
  • i just want to return true if not duplicates and false for duplicate exists Commented Jul 31, 2018 at 9:58
  • so, return true where you console.log ... return false outside of the loops Commented Jul 31, 2018 at 10:00
  • new Set(array).size === array.length is true if all array items are unique Commented Jul 31, 2018 at 10:03
  • a.map(item=> item .split(',').map(child => child)).map(item => new Set(item).size === item.length) try by doing this may be this solves your problem Commented Jul 31, 2018 at 10:18

2 Answers 2

3

The items in a Set will always be unique, as it only keeps one copy of each value you put in. Here's a function that uses this property:

function hasDuplicates(iterable) {
  return  new Set(iterable).size !== iterable.length;
}

// Demo
var array = ["Saturday", "Sunday", "Monday", "Tuesday", "Saturday" ];
console.log(hasDuplicates(array))

// Returns True if there is duplicates
// Otherwise return false

Set(iterable).size will return the count of unique elements in that set, while iterable.length is the counts of all elements in the original array.

Edit #1

To check ONLY the first item for duplicates you can use something like this

var array = ["Saturday",  "Saturday", "Sunday", "Monday", "Tuesday" ];


console.log(array.indexOf(array[0] , 1) === -1 ? "No duplicates" : "Has duplicate" );

Sign up to request clarification or add additional context in comments.

5 Comments

i need to check only first value of comma separated values for each item in the array for duplicates
please consider the second array in the question
Which second array? there is only one in your question!
Array(10) 0: "test3,tier 1,test,test2 "1: "test3,tier 1,test,test2 "2: "test3,tier 1,test,test2 "3: "test3,tier 1,test,test2 "4: "test3,tier 1,test,test2 "5: "test3,tier 1,test,test2 "6: "test3,tier 1,test,test2 "7: "test3,tier 1,test,test2 "8: "test3,tier 1,test,test2 "9: "test3,tier 1,test,test2 "length: 10
Thanks for helping, i made the final solution here as another answer
0
// Check duplicates in upload data excel

    checkDuplicates(arr) {

        if (arr.length > 1) {
            for (var i = 0; i < arr.length; i++) {
                for (var j = i + 1; j < arr.length; j++) {

                    if (arr[i].split(',')[0] === arr[j].split(',')[0]) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        } else {
            return false;
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.