-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnion of 2 Sorted with Duplicates.cpp
50 lines (44 loc) · 1.24 KB
/
Union of 2 Sorted with Duplicates.cpp
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
class Solution {
public:
// a,b : the arrays
// Function to return a list containing the union of the two arrays.
vector<int> findUnion(vector<int> &a, vector<int> &b) {
// Your code here
// return vector with correct order of elements
int n1 = a.size(), n2 = b.size(), i = 0, j=0;
set<int> mySet; int curr = INT_MIN;
vector<int> result;
while(i < n1 && j < n2){
if (curr == a[i]){
i++; continue;
}
if(curr == b[j]){
j++; continue;
}
if(a[i] <= b[j]){
curr = a[i];
result.push_back(a[i]);
i++;
} else{
curr = b[j];
result.push_back(b[j]);
j++;
}
}
while(i < n1){
if(curr == a[i]){
i++; continue;
}
curr = a[i];
result.push_back(a[i]);i++;
}
while(j < n2){
if(curr == b[j]){
j++; continue;
}
curr = b[j];
result.push_back(b[j]);j++;
}
return result;
}
};