-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathArraysUnion.java
82 lines (69 loc) · 2.64 KB
/
ArraysUnion.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package by.andd3dfx.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
/**
* Даны 2 упорядоченных массива с уникальными элементами,
* найти и вывести их упорядоченное объединение без дубликатов.
*
* @see <a href="https://youtu.be/uABGnnTD0Kw">Video solution</a>
*/
public class ArraysUnion {
public static int[] unite(int[] a, int[] b) {
List<Integer> result = new ArrayList<>();
int aIndex = 0;
int bIndex = 0;
Integer lastAdded = null;
// Process overlapping part of arrays
while (aIndex < a.length && bIndex < b.length) {
if (a[aIndex] <= b[bIndex]) {
var itemToAdd = a[aIndex];
lastAdded = addItemToArray(result, itemToAdd, lastAdded);
aIndex++;
} else {
var itemToAdd = b[bIndex];
lastAdded = addItemToArray(result, itemToAdd, lastAdded);
bIndex++;
}
}
// Add the remaining part of one of the arrays
Arrays.stream(Arrays.copyOfRange(a, aIndex, a.length))
.forEach(result::add);
Arrays.stream(Arrays.copyOfRange(b, bIndex, b.length))
.forEach(result::add);
return result.stream()
.mapToInt(value -> value)
.toArray();
}
private static Integer addItemToArray(List<Integer> array, int itemToAdd, Integer lastAdded) {
if (lastAdded == null || itemToAdd != lastAdded) {
array.add(itemToAdd);
lastAdded = itemToAdd;
}
return lastAdded;
}
public static int[] uniteUsingSet(int[] a, int[] b) {
LinkedHashSet<Integer> result = new LinkedHashSet<>();
int aIndex = 0;
int bIndex = 0;
// Process overlapping part of arrays
while (aIndex < a.length && bIndex < b.length) {
if (a[aIndex] <= b[bIndex]) {
result.add(a[aIndex]);
aIndex++;
} else {
result.add(b[bIndex]);
bIndex++;
}
}
// Add the remaining part of one of the arrays
Arrays.stream(Arrays.copyOfRange(a, aIndex, a.length))
.forEach(result::add);
Arrays.stream(Arrays.copyOfRange(b, bIndex, b.length))
.forEach(result::add);
return result.stream()
.mapToInt(value -> value)
.toArray();
}
}