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