-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathmergeTwo.java
29 lines (27 loc) · 1.1 KB
/
mergeTwo.java
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
/* Start with two arrays of strings, A and B, each with its elements in
* alphabetical order and without duplicates. Return a new array containing
* the first N elements from the two arrays. The result array should be in
* alphabetical order and without duplicates. A and B will both have a length
* which is N or more. The best "linear" solution makes a single pass over A
* and B, taking advantage of the fact that they are in alphabetical order,
* copying elements directly to the new array.
*/
public String[] mergeTwo(String[] a, String[] b, int n) {
String[] arr = new String[n];
int aIndex = 0;
int bIndex = 0;
for(int index = 0; index < n; index++) {
if(a[aIndex].compareTo(b[bIndex]) < 0) {
arr[index] = a[aIndex];
aIndex++;
} else if(a[aIndex].compareTo(b[bIndex]) > 0) {
arr[index] = b[bIndex];
bIndex++;
} else {
arr[index] = a[aIndex];
aIndex++;
bIndex++;
}
}
return arr;
}