skip to Main Content

I have the following JS arrays.

$arrTest1 = ['46819', '46819', '46819'];
$arrTest2 = ['46819', '46802', '46819'];

let len1 = arrTest1.length;
let len2 = $.unique(arrTest1).length; 

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


let len3 = arrTest2.length;
let len4 = $.unique(arrTest2).length; 

if (len3 > len4) {
  alert('Found duplicate');
} else {
  alert('Did not find duplicate');
}

For first array $arrTest1 its working and showing found duplicate but for $arrTest2 its showing as Did not find duplicate. Did I missed something?

$.unique is not working for $arrTest2 whereas for $arrTest1 it works fine.

3

Answers


  1. Documentation:

    Description: Sorts an array of DOM elements, in place, with the
    duplicates removed. Note that this only works on arrays of DOM
    elements, not strings or numbers.

    $arrTest1 = ['46819', '46819', '46819'];
    $arrTest2 = ['46819', '46802', '46819'];
    
    let len1 = $arrTest1.length;
    let len2 = [...new Set($arrTest1)].length; 
    
    if (len1 > len2) {
      alert('Found duplicate');
    } else {
      alert('Did not find duplicate');
    }
    
    
    let len3 = $arrTest2.length;
    let len4 = [...new Set($arrTest2)].length; 
    
    if (len3 > len4) {
      alert('Found duplicate');
    } else {
      alert('Did not find duplicate');
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    You can use Set like my example

    Login or Signup to reply.
  2. You can use

    1. $.merge() is used to combine array1 and array2 into combinedArray.
    2. $.grep() is used to filter combinedArray and find the duplicate values, which are stored in duplicateValues.

    Refer below Code

    $arrTest1 = ['46819', '46819', '46819'];
    $arrTest2 = ['46819', '46802', '46819'];
    
    const combinedArray = $.merge($.merge([], $arrTest1), $arrTest2);
    
    // Find duplicate values
    const duplicateValues = $.grep(combinedArray, function (element, index) {
      return index !== $.inArray(element, combinedArray);
    });
    
    if(duplicateValues.length) {
      alert('Found duplicate');
    } else {
      alert('Did not find duplicate');
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Login or Signup to reply.
  3. $.unique has be removed in V.3.0 and above you can do it using $.grep()

    var arrTest1 = ['46819', '46819', '46819'];
    var arrTest2 = ['46819', '46802', '46819'];
    
    var duplicatesTest1 = $.grep(arrTest1, function (e, i) {
        return $.inArray(e, arrTest1) !== i;
    });
    
    if (duplicatesTest1.length > 0) {
        alert('Found duplicate in arrTest1');
    } else {
        alert('Did not find duplicate in arrTest1');
    }
    
    var duplicatesTest2 = $.grep(arrTest2, function (e, i) {
        return $.inArray(e, arrTest2) !== i;
    });
    
    if (duplicatesTest2.length > 0) {
        alert('Found duplicate in arrTest2');
    } else {
        alert('Did not find duplicate in arrTest2');
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search