forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnion of two arrays.cpp
51 lines (43 loc) · 1.21 KB
/
Union of two arrays.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
51
/*
Union of two arrays
===================
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.
Example 1:
Input:
5 3
1 2 3 4 5
1 2 3
Output: 5
Explanation:
1, 2, 3, 4 and 5 are the
elements which comes in the union set
of both arrays. So count is 5.
Example 2:
Input:
6 2
85 25 1 32 54 6
85 2
Output: 7
Explanation:
85, 25, 1, 32, 54, 6, and
2 are the elements which comes in the
union set of both arrays. So count is 7.
Your Task:
Complete doUnion funciton that takes a, n, b, m as parameters and returns the count of union elements of the two arrays.The printing is done by the driver code.
Constraints:
1 ≤ N, M ≤ 105
1 ≤ A[i], B[i] < 105
Expected Time Complexity : O((n+m)log(n+m))
Expected Auxilliary Space : O(n+m)
*/
// a and b : given array with n and m size respectively
int doUnion(int a[], int n, int b[], int m)
{
unordered_set<int> u;
for (int i = 0; i < n; ++i)
u.insert(a[i]);
for (int i = 0; i < m; ++i)
u.insert(b[i]);
return u.size();
}