Created
March 30, 2018 22:46
-
-
Save eldenis/22791881c9a32cff173ec117655ea380 to your computer and use it in GitHub Desktop.
Interview question. Intersection between two arrays.
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
static IEnumerable<int> Intersection(int[] a, int[] b) | |
{ | |
int i = 0, j = 0; | |
while (i < a.Length && j < b.Length) | |
{ | |
if (a[i] < b[j]) | |
{ | |
i++; | |
continue; | |
} | |
if (b[j] < a[i]) | |
{ | |
j++; | |
continue; | |
} | |
if (a[i] != b[j]) continue; | |
yield return a[i]; | |
i++; | |
j++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know much, but this to me looks a perfectly acceptable answer.