determine if array has duplicates lodash 1

determine if array has duplicates lodash

function hasDuplicates(a) {
  return _.uniq(a).length !== a.length; 
}

var a = [1,2,1,3,4,5];
var b = [1,2,3,4,5,6];

document.write(hasDuplicates(a), ',',hasDuplicates(b));

Here is what the above code is Doing:
1. We’re creating a function called hasDuplicates that takes an array as an argument.
2. We’re using the uniq method from the Underscore library to create a new array that contains only the unique elements from the array passed to the function.
3. We’re comparing the length of the original array to the length of the new array. If they’re the same, then there are no duplicates. If they’re different, then there are duplicates.
4. We’re returning the result of the comparison.
5. We’re creating two arrays, a and b.
6. We’re calling the hasDuplicates function on each array and writing the result to the document.

Similar Posts