Skip to content

Instantly share code, notes, and snippets.

@oliverralbertini
Last active October 25, 2017 20:12
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 oliverralbertini/600728ce8a59ce331bf3f1e60f977719 to your computer and use it in GitHub Desktop.
Save oliverralbertini/600728ce8a59ce331bf3f1e60f977719 to your computer and use it in GitHub Desktop.
Finds the intersection of two sorted lists without duplicates
class IntersectionTwoSortedLists {
public static void main(String[] args) {
int[] a1 = { 2, 3, 3, 4, 6, 6, 8 };
int[] a2 = { 3, 3, 6, 7, 9 };
for (int c1 = 0, c2 = 0; c1 < a1.length && c2 < a2.length; ) {
if (a1[c1] < a2[c2]) c1++;
else if (a1[c1] > a2[c2]) c2++;
else {
if (c1 != a1.length - 1 && a1[c1] == a1[c1 + 1])
c1++;
else if (c2 != a2.length - 1 && a2[c2] == a2[c2 + 1])
c2++;
else {
System.out.print(a1[c1] + " ");
c1++;
c2++;
}
}
}
System.out.println();
}
}
// prints out 3 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment