I have two arrays like this:
var x = ['1','2','6'];
var y = ['4', '5','6'];
How do I find duplicates in two arrays in pure JavaScript and I would like to avoid using a loop?
Output - duplicates: 6
I have two arrays like this:
var x = ['1','2','6'];
var y = ['4', '5','6'];
How do I find duplicates in two arrays in pure JavaScript and I would like to avoid using a loop?
Output - duplicates: 6
Try something like this:
var x = ['1','2','6'];
var y = ['4', '5','6'];
var overlap = x.filter(function(v,i,a){
return y.indexOf(v) > -1;
});
console.log(overlap); // ['6']
Does this work for your purpose?
Try this
var x = ['1','2','6'];
var y = ['4', '5','6'];
var duplicate = [];
for (var i=0; i<y.length; i++) {
var index = x.indexOf(y[i]);
if (index > -1) {
duplicate.push(x[index]);
}
}
Output: ["6"]