Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  83] [ 6]  / answers: 1 / hits: 40383  / 9 Years ago, wed, february 11, 2015, 12:00:00

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.


More From » lodash

 Answers
4

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>




[#67864] Monday, February 9, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarissakourtneyb

Total Points: 710
Total Questions: 89
Total Answers: 125

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
;