Skip to content

Instantly share code, notes, and snippets.

@jiggzson
Last active September 21, 2016 00:54
Show Gist options
  • Save jiggzson/dc257ac1468f4a7a4ff96f229eac4d87 to your computer and use it in GitHub Desktop.
Save jiggzson/dc257ac1468f4a7a4ff96f229eac4d87 to your computer and use it in GitHub Desktop.
/**
* Gets the intersection of two arrays
* @param a An array
* @param b An array
* @returns {Array}
*/
function intersection(a, b) {
b = b.slice();
var c = [];
if(a.length > b.length) {
var t = a; a = b; b = t;
}
var l = a.length, l2 = b.length;
for(var i=0; i<l; i++) {
var item = a[i];
for(var j=0; j<l2; j++) {
var item2 = b[j];
if(item2 === undefined) continue;
if(item === item2) {
b[j] = undefined;
c.push(item);
continue;
}
}
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment