Last active
August 29, 2015 14:25
-
-
Save dmi3y/c481c0d7e2c30c31820c to your computer and use it in GitHub Desktop.
Find intersections
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
// http://jsbin.com/mapena/edit?js,console | |
// http://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript | |
function findIntersctions(a, b) { | |
var ai = 0; | |
var bi = 0; | |
var out = []; | |
while( ai < a.length && bi < b.length ) { | |
if( a[ai] > b[bi] ) { | |
bi += 1; | |
} else if ( a[ai] < b[bi] ) { | |
ai += 1; | |
} else { | |
out.push(a[ai]); | |
ai += 1; | |
bi += 1; | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment