forked from andrei-punko/java-interview-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindElementsOfArrayWhichNotPresentInAnotherArray.java
More file actions
46 lines (40 loc) · 1.4 KB
/
FindElementsOfArrayWhichNotPresentInAnotherArray.java
File metadata and controls
46 lines (40 loc) · 1.4 KB
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
package by.andd3dfx.search;
import java.util.ArrayList;
import java.util.List;
/**
* <pre>
* Даны два отсортированных массива (A1,A2...AN), (B1,B2...BN), значения могут быть отрицательными, могут повторяться.
* Вернуть элементы первого массива, которых нет во втором массиве.
*
* input {-1, 2, 2, 5, 7} {2, 5, 8, 8, 8}
* output {-1, 7}
* </pre>
*
* @see <a href="https://youtu.be/bJIzoT-CmUg">Video solution</a>
*/
public class FindElementsOfArrayWhichNotPresentInAnotherArray {
public static Integer[] find(Integer[] a, Integer[] b) {
int i = 0;
int j = 0;
List<Integer> result = new ArrayList<>();
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
result.add(a[i]);
i++;
} else if (a[i] > b[j]) {
result.add(b[j]);
j++;
} else {
var element = a[i];
while (i < a.length && a[i] == element) i++;
while (j < b.length && b[j] == element) j++;
}
}
// Add remaining part of a[]
while (i < a.length) {
result.add(a[i]);
i++;
}
return result.toArray(new Integer[0]);
}
}