35

What do you all think would be the best (best can be interpreted as most readable or most performant, your choice) way to write a function using the lodash utilities in order to check an array for duplicate values.

I want to input ['foo', 'foo', 'bar'] and have the function return true. And input ['foo', 'bar', 'baz'] and have the function return false.

6 Answers 6

69

You can try this code:

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));
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.1.0/lodash.min.js"></script>

5
  • Any particular reason for == vs ===? Feb 11, 2015 at 18:03
  • 1
    It does not matter in this particular case. I changed to ===. Thank you!
    – agershun
    Feb 11, 2015 at 18:06
  • 1
    Sure, but to be picky the OP asked for a hasDuplicates function, not hasNoDuplicates.
    – user663031
    Feb 11, 2015 at 18:37
  • 1
    Changed to hasDuplicates()
    – agershun
    Feb 11, 2015 at 19:56
  • Short and accurate. Great Mar 27, 2017 at 5:34
19

As of ES6 you can simply use Set so this becomes:

let hasDuplicates = arr => new Set(arr).size != arr.length

console.log(hasDuplicates([5,3,2,1,2,1,2,1]))
console.log(hasDuplicates([1,2,3,4,5]))

Which somewhat negates the use of lodash in this particular case.

7

You could check that there is _.some element in the array which does not return its own location when looked up in the array. In other words, there is at least one element which has a match earlier in the array.

function hasDuplicates(array) {
    return _.some(array, function(elt, index) { 
        return array.indexOf(elt) !== index; 
    });
}

Perhaps this is faster than the _.uniq solution, since it will identify the first duplicated element right away without having to compute the entire unique-ified array.

Or, depending on your coding style and desire for readability, and if you want to use ES6 arrow functions for brevity:

var earlierMatch  = (elt, index, array) => array.indexOf(elt) !== index;
var hasDuplicates = array => _.some(array, earlierMatch);
3
  • great use of indexOf!!
    – Hrishi
    Feb 11, 2015 at 18:40
  • Nit-pick: indexOf (O(n)) within a some (O(n)) -> O(n2).
    – tokland
    Feb 5, 2018 at 12:07
  • 2
    Yeah, this is an O(n^2) algorithm for checking for duplicates. I wouldn't call that a nitpick, more of a major flaw. May 30, 2018 at 19:22
3

Well, there's always. lodash's _.uniq() function. That function actually returns a new array that only contains unique values, so checking to see if the length of the array has changed would get you your 'true' or 'false' value to return yourself, I believe.

2

I don't know lodash but I submit:

_.any(_.countBy(['foo', 'foo', 'bar']), function(x){ return x > 1; });

The problem with all the solutions proposed so far is that the entire input array needs processing to get an answer, even if the answer is obvious from the first 2 elements of the array.

1
  • As of version 4 of lodash, _.any is deprecated in favor of _.some Sep 10, 2018 at 22:06
0

No need to use lodash, use following code instead:

function getDuplicates(array, key) {
  return array.filter(e1=>{
    if(array.filter(e2=>{
      return e1[key] === e2[key];
    }).length > 1) {
      return e1;
    }
  })
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.