# Checking two arrays for matches

Is there a better way to do this?

    var duplicate = false;
for (var i = 0; i < acceptedFiles.length; i++) {
for (var j = 0; j < attachments.attachments.length; j++) {
if (attachments.attachments[j].name === acceptedFiles[i].name) {
duplicate = true;
}
}
}

• You should keep a look out for Set this would allow you to build a set of your acceptedFiles and pass that around. Thus you could remove the outer for loop and simply query the set if the attachment is accepted. If you have many accepted types, this could also speed up significantly as you go from $\mathcal{O}(nk)$ to $\mathcal{O}(n)$. Nov 12, 2014 at 15:48

Presumably, once a duplicate is found you don't need to continue looping:

if (attachments.attachments[j].name === acceptedFiles[i].name) {
duplicate = true;
}


With JavaScript, the best thing would be to wrap up your for loops in a function:

function containsDuplicate(acceptedFiles, attachments) {
for (var i = 0; i < acceptedFiles.length; i++) {
for (var j = 0; j < attachments.attachments.length; j++) {
if (attachments.attachments[j].name === acceptedFiles[i].name) {
return true;
}
}
}
return false;
}


As this is a problem that applies to multiple languages, look at the break keyword.

• @anorton I can't quite wrap my head around how that works here. When i = 1 we are missing the comparison of x[1] to y[0]? Nov 12, 2014 at 17:18
• Oops. I read attachments and acceptedFiles as the same array. If we were looking for duplicates in the same array, it works. sorry. Nov 12, 2014 at 17:20
• @anorton Ahhhhhh that makes sense now Nov 12, 2014 at 17:21

Using ES5 you can generally avoid having to do tedious for loops to iterate through the values. In this case you can use the some method on arrays in order avoid using a flag and having i and j iterators to get the array values. You also get the added bonus of some short circuiting and stopping when the first true is found so as far as big O notation it is more efficient.

function hasDuplicate() {
return acceptedFiles.some(function(acceptedValue) {
return attachments.attachments.some(function(attachmentValue) {
return acceptedValue.name === attachmentValue.name;
});
});
}


If your browser doesn't support ES5 you can use a shim

If using external libraries is an option, then LoDash would allow for something more like:

var duplicate = _.intersection(_.pluck(acceptedFiles, 'name'),
_.pluck(attachments, 'name')).length > 0;


or:

function areDuplicates(acceptedFiles, attachments) {
var acceptedFileNames = _.pluck(acceptedFiles, 'name');
var attachmentNames = _.pluck(attachments, 'name');
return _.intersection(acceptedFileNames, attachmentNames).length > 0;
}

• I like this (and it's more concise so +1), but in terms of performance, it's probably less efficient than the OP's existing code Nov 12, 2014 at 16:32

You can return as soon as a match is found.

for (var i = 0; i < acceptedFiles.length; i++) {
for (var j = 0; j < attachments.attachments.length; j++) {
if (attachments.attachments[j].name === acceptedFiles[i].name) {
return true;
}
}
}