Skip to content

Instantly share code, notes, and snippets.

@dmi3y
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3y/c481c0d7e2c30c31820c to your computer and use it in GitHub Desktop.
Save dmi3y/c481c0d7e2c30c31820c to your computer and use it in GitHub Desktop.
Find intersections
// 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