Created
July 9, 2011 18:32
-
-
Save mjackson/1073830 to your computer and use it in GitHub Desktop.
Get the intersection of two sorted arrays of numbers or strings.
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
// 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