-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunionOfTwoArrays.cpp
More file actions
53 lines (50 loc) · 1.58 KB
/
unionOfTwoArrays.cpp
File metadata and controls
53 lines (50 loc) · 1.58 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
47
48
49
50
51
52
53
/* Q-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 */
#include <bits/stdc++.h>
using namespace std;
int doUnion(int *, int , int *, int);
int main() {
int n, m;
cin >> n >> m;
int a[n], b[m];
for(int i = 0;i<n;i++)
cin >> a[i];
for(int i = 0;i<m;i++)
cin >> b[i];
cout << doUnion(a, n, b, m) << endl;
return 0;
}
// a and b : given array with n and m size respectively
int doUnion(int a[], int n, int b[], int m) {
int un[m+n],u=0,i,j,c;
//iterating first array
for(j=0;j<n;j++)
{
//iterating unioun array
for(i=0;i<u;i++)
{
//checking if element at j index is already present in union array or not,if found present then break loop
if(a[j]==un[i])
break;
}
//checking if the element is found or not,if not found then adding it in unioun array
if(i==u)
un[u++]=a[j];
}
//iterating first array
for(j=0;j<m;j++)
{
//iterating unioun array
for(i=0;i<u;i++)
{
//checking if element at j index is already present in union array or not,if found present then break loop
if(b[j]==un[i])
break;
}
//checking if the element is found or not,if not found then adding it in unioun array
if(i==u)
un[u++]=b[j];
}
return u;
}