-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersect.java
44 lines (31 loc) · 1.01 KB
/
intersect.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
/*
Find the intersection of two sorted arrays.
OR in other words,
Given 2 sorted arrays, find all the elements which occur in both the arrays.
Example :
Input :
A : [1 2 3 3 4 5 6]
B : [3 3 5]
Output : [3 3 5]
Input :
A : [1 2 3 3 4 5 6]
B : [3 5]
Output : [3 5]
*/
public ArrayList<Integer> intersect(final List<Integer> a, final List<Integer> b) {
if(a==null || b==null || a.isEmpty() || b.isEmpty())
return null;
ArrayList<Integer> result = new ArrayList<Integer>();
int first=0; int second=0;
while(first < a.size()&& second < b.size())
{
if(a.get(first).equals(b.get(second))) {
result.add(a.get(first));
first++; second++;
}
else if(a.get(first)>b.get(second))
second++;
else first++;
}
return result;
}