Jquery check if array contains duplicate string

how to check duplicate values in jquery
javascript array check duplicate before push
jquery remove duplicates from array of objects
check if value already exists in array jquery
typescript find duplicate objects in array
check if array has duplicate values javascript
javascript count duplicates in array
check if value already exists in array javascript

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;
}

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.

Check if value exists in Array - jQuery and JavaScript, Check if value exists in Array – jQuery and JavaScript. In this tutorial, I indexOf('​vishal') ); // Search in String console.log( 'Index : ' + text.indexOf('a') ); Using this you can remove or replace the duplicate values. If you are  Note that inArray returns the index of the element found, so 0 indicates the element is the first in the array. -1 indicates the element was not found. $.inArray is effectively a wrapper for Array.prototype.indexOf in browsers that support it (almost all of them these days), while providing a shim in those that don’t.

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 )

JavaScript Includes To Search In Strings & Arrays, How do you check if there are duplicates in an array JavaScript? I discussed some of the methods which you can use to search your value in the existing Array. Using this you can remove or replace the duplicate values. If you are using jQuery within your project then you can simply go with inArray() method otherwise you can indexOf() method. If you found this tutorial helpful then don't forget to share.

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

jQuery.unique(), How do you find if an array contains a specific string in JavaScript? Check if a given array contains duplicate elements within k distance from each other Given an unsorted array that may contain duplicates. Also given a number k which is smaller than size of array. Write a function that returns true if array contains duplicates within k distance.

just use unique() method.

$.unique(reportRecipients);

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

JavaScript array: Find duplicate values in a array, The $. unique() function searches through an array of objects, sorting the array, and removing any duplicate nodes. A node is considered a duplicate if it is the exact same node as one already in the array; two different nodes with identical attributes are not considered to be duplicates. If the first element within the array matches value, $.inArray() returns 0. Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), to check for the presence of value within array, you need to check if it's not equal to (or greater than) -1. The comparison between values is strict.

Calculate If Duplicates Exist In An Array Using JavaScript, Write a JavaScript program to find duplicate values in a JavaScript for (var prop in object) { if(object[prop] >= 2) { result.push(prop); } } return  jQuery.inArray () This jQuery array method search the the item within the array. If element exists in the jQuery array it returns the index position of the value and if the value doesn’t exist then it will return -1. jQuery.inArray() method works with the both string and an array.

(remove duplicates) in a JavaScript array?, Use JavaScript to calculate if duplicate values exist in a single array. In the above code, we are taking in an array of values, string or integer is fine. When we hit a count key that already exists, it is because the value is a  $.unique(array) The unique() function remove duplicate values from array in jQuery. That meanse it returns an array of the unique elements in the original array. The below code return unique and sorted array.

How to check if array contains duplicate values in javascript, How to detect HTTP or HTTPS then force redirect to HTTPS in JavaScript ? How to Given an array which contains some values, the task is to remove the duplicate If the current value does not exist in the output array with unique values, then add the Example 1: This example generates an unique array of string values. Check if array contains contiguous integers with duplicates allowed Given an array of n integers(duplicates allowed). Print “Yes” if it is a set of contiguous integers else print “No”.

Comments
  • possible duplicate of Easiest way to find duplicate values in a JavaScript array
  • does it work for string also..
  • Yes it does work for string as well.