Last active
October 25, 2017 20:12
-
-
Save oliverralbertini/600728ce8a59ce331bf3f1e60f977719 to your computer and use it in GitHub Desktop.
Finds the intersection of two sorted lists without duplicates
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
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