Skip to content

Instantly share code, notes, and snippets.

@eldenis
Created March 30, 2018 22:46
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 eldenis/22791881c9a32cff173ec117655ea380 to your computer and use it in GitHub Desktop.
Save eldenis/22791881c9a32cff173ec117655ea380 to your computer and use it in GitHub Desktop.
Interview question. Intersection between two arrays.
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++;
}
}
@eldenis
Copy link
Author

eldenis commented Mar 30, 2018

I don't know much, but this to me looks a perfectly acceptable answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment