Last active
September 21, 2016 00:54
-
-
Save jiggzson/dc257ac1468f4a7a4ff96f229eac4d87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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