-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueDriver.java
More file actions
42 lines (33 loc) · 936 Bytes
/
UniqueDriver.java
File metadata and controls
42 lines (33 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class UniqueDriver {
public static void main(String[] args) {
int[] A = { 1, 3, 7 };
int[] B = { 3, 4, 7, 8, 9 };
int unique = unique(A, B);
System.out.println("You have " + unique + " unique numbers in your two arrays" );
}
static int unique(int[] A, int[] B) {
int j = Math.min(A.length-1, B.length-1);
int count = 0;
int idxA = 0;
int idxB = 0;
for (int i = 0; i < j; i++) {
// Find every unique in A until we bypass the value in B
while (A[idxA] < B[idxB]) {
count++;
idxA++;
}
// Find every unique in B until we bypass the value in A
while (B[idxB] < A[idxA]) {
count++;
idxB++;
}
// Equal numbers, increase the index on both Arrays
if (A[idxA] == B[idxB]) {
idxA++;
idxB++;
}
}
// Return the unique we found so far + remaining elements in A + remaining elements in B
return count + (A.length - idxA) + (B.length - idxB);
}
}