Skip to content

Instantly share code, notes, and snippets.

@mjackson
Created July 9, 2011 18:32
Show Gist options
  • Save mjackson/1073830 to your computer and use it in GitHub Desktop.
Save mjackson/1073830 to your computer and use it in GitHub Desktop.
Get the intersection of two sorted arrays of numbers or strings.
// Returns the intersection of two sorted arrays of numbers or strings.
function intersectArrays(a, b) {
var array = [], ai = 0, alen = a.length, bi = 0, blen = b.length;
while (ai < alen && bi < blen) {
if (a[ai] < b[bi]) {
ai++;
} else if (a[ai] > b[bi]) {
bi++;
} else {
array.push(a[ai]);
ai++;
bi++;
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment