DEV Community

Discussion on: And then the interviewer asks, "Can you do this with less code?"

Collapse
 
jochemstoel profile image
Jochem Stoel

You can do it with less code, the parenthesis can be omitted here. :P

const duplicateCheck = a => new Set(a).size !== a.length;
Collapse
 
elmuerte profile image
Michiel Hendriks

If you are going to assume a is an array, then you could even use !=.

Collapse
 
michaelsolati profile image
Michael Solati

Haha, I thought so too. And I guess we could use let instead of const since it's one letter less.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️
const duplicateCheck = a => a.length - new Set(a).size;

Shorter and more useful since it returns the number of duplicates

Thread Thread
 
elmuerte profile image
Michiel Hendriks • Edited
const duplicateCount = a => a.length - new Set(a).size;

And now with a meaningful name. Because, what does duplicateCheck mean, that is has, or does not have duplicates. For the boolean version hasDuplicates would be the better name.