3

How to check if array contains a duplicate string , i have validateArray = ['sa','sa','yu'] i have used the following function from SO but same not working for me.

checkDuplicate = function (reportRecipients) {
    if (reportRecipients.length > 1) {
        var recipientsArray = reportRecipients.toString().split(',');
        for (a in recipientsArray) {
            if(reportRecipients.indexOf(a) != reportRecipients.lastIndexOf(a)){
                return true;
                break;
            }
        }
    }
    return false;
}
3

7 Answers 7

27

This is working for me:

var reportRecipients = ['AAA', 'XYZ', 'AAA', 'ABC', 'XXX', 'XYZ', 'PQR'];
var recipientsArray = reportRecipients.sort(); 

var reportRecipientsDuplicate = [];
for (var i = 0; i < recipientsArray.length - 1; i++) {
    if (recipientsArray[i + 1] == recipientsArray[i]) {
        reportRecipientsDuplicate.push(recipientsArray[i]);
    }
}

Hope that helps.

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

Comments

1

Please try this, if the sort function is not working well :

    var all_list= ['sa','sa','yu'] 
    var duplicates_list = [];
    var unique_list = [];
    $.each(all_list, function(key, value){
            if($.inArray(value, unique_list ) == -1){
                unique_list.push(value);
            }else{
                if($.inArray(value, duplicates_list ) == -1){
                    duplicates_list.push(value);
                }
            }
    });

//duplicated elements 
console.log(duplicates_list )

Comments

1

You can check if an array has duplicates using $.unique function on jQuery.

let len1 = myArray.length; //get the length of your array
let len2 = $.unique(myArray).length; //the length of array removing the duplicates

if (len1 > len2) {
  alert('Found duplicate');
} else {
  alert('Did not find duplicate');
}

1 Comment

This does not work, it is meant for DOM objects. See api.jquery.com/jQuery.uniqueSort
0

Take a look at this:

function getDistinctArray(arr) {
    var compareArray = new Array();
    if (arr.length > 1) {
        for (i = 0;i < arr.length;i++) {
            if (compareArray.indexOf(arr[i]) == -1) {
                compareArray.push(arr[i]);
            }
        }
    }
    return compareArray;
}

And here is a working fiddle

Comments

0
    var arr = [1, 2, 3, 4, 4];
    var chk = new Set();
    var duplicates = arr.filter(n => chk.size === chk.add(n).size);
    //console.log(duplicates); // [ 5 ]
    //console.log(duplicates.length); // 1
    if(duplicates.length > 0){
        alert('Duplicate Found');
        return false;
    }
    else{
        return true;
    }

This Working fine. it is the best shortest method .

Comments

0

A simple and clean solution

var fileredArray = myArray.filter(function (elem, index, self) {
    return index === self.indexOf(elem);
});

Demo:

var myArray = [10, 20, 30, 10];

//also works with strings (case sensitive!)
var myArray = ['Amsterdam', 'Paris', 'London', 'Paris'];

//check for duplicates
var fileredArray = myArray.filter(function (elem, index, self) {
    return index === self.indexOf(elem);
});

//show warning if 1 or more duplicate found
if (fileredArray.length < myArray.length) {
    alert('Duplicates found.')
}

//logging
console.log(myArray);
console.log(fileredArray);

Comments

-6

just use unique() method.

$.unique(reportRecipients);

https://api.jquery.com/jQuery.unique/

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.