-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUnionAndIntersectionOfTwoSortedArrays.js
139 lines (131 loc) · 3.79 KB
/
UnionAndIntersectionOfTwoSortedArrays.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @author Anirudh Sharma
* <p>
* Given two arrays A and B of size N and M respectively. The task is to find union between these two arrays.
* Union of the two arrays can be defined as the set containing distinct elements from both the arrays.
* If there are repetitions, then only one occurrence of element should be printed in union.
*/
const findUnion = (a, b) => {
// Array to store union of two arrays
const union = [];
// Lengths of two arrays
const m = a.length;
const n = b.length;
// Counters for both arrays
let i = 0;
let j = 0;
// Loop until elements left in either array
while (i < m && j < n) {
// If a has smaller element then put it in the list
if (a[i] < b[j]) {
union.push(a[i]);
i++;
// Handle for duplicate elements
while (i < m && a[i] === a[i - 1]) {
i++;
}
}
// If b has smaller element then put it in the list
else if (a[i] > b[j]) {
union.push(b[j]);
j++;
// Handle for duplicate elements
while (j < n && b[j] === b[j - 1]) {
j++;
}
}
// If both elements are same, then put either of them
else {
union.push(a[i]);
i++;
j++;
// Handle for duplicate elements
while (i < m && a[i] === a[i - 1]) {
i++;
}
while (j < n && b[j] === b[j - 1]) {
j++;
}
}
}
// Add all remaining elements from a
while (i < m) {
union.push(a[i]);
i++;
// Handle for duplicate elements
while (i < m && a[i] === a[i - 1]) {
i++;
}
}
// Add all remaining elements from b
while (j < n) {
union.push(b[j]);
j++;
// Handle for duplicate elements
while (j < n && b[j] === b[j - 1]) {
j++;
}
}
return union;
};
const findIntersection = (a, b) => {
// Array to store the intersection of two arrays
const intersection = []
// Length of two arrays
const m = a.length;
const n = b.length;
// Indices for two arrays
let i = 0;
let j = 0;
// Loop until there is element left in either array
while (i < m && j < n) {
if (a[i] < b[j]) {
i++;
// Handle duplicate elements
while (i < m && a[i] === a[i - 1]) {
i++;
}
} else if (a[i] > b[j]) {
j++;
// Handle duplicate elements
while (j < n && b[j] === b[j - 1]) {
j++;
}
} else {
intersection.push(a[i]);
i++;
j++;
while (i < m && a[i] === a[i - 1]) {
i++;
}
while (j < n && b[j] === b[j - 1]) {
j++;
}
}
}
return intersection;
};
let A = [1, 2, 3, 4, 5];
let B = [1, 2, 3];
console.log(`Union: ${findUnion(A, B)}`);
console.log(`Intersection: ${findIntersection(A, B)}`);
A = [1, 2, 3, 4, 5];
B = [6, 7, 8];
console.log(`Union: ${findUnion(A, B)}`);
console.log(`Intersection: ${findIntersection(A, B)}`);
A = [1, 1, 1, 1, 1, 1];
B = [2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6];
console.log(`Union: ${findUnion(A, B)}`);
console.log(`Intersection: ${findIntersection(A, B)}`);
A = [1, 3, 5, 7, 9];
B = [2, 4, 6, 8];
console.log(`Union: ${findUnion(A, B)}`);
console.log(`Intersection: ${findIntersection(A, B)}`);
A = [1, 2, 3, 4, 5, 5, 6];
B = [1, 2, 3, 4, 5, 5, 6, 6, 7];
console.log(`Union: ${findUnion(A, B)}`);
console.log(`Intersection: ${findIntersection(A, B)}`);
A = [];
B = [];
console.log(`Union: ${findUnion(A, B)}`);
console.log(`Intersection: ${findIntersection(A, B)}`);