-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathintersection_2array.cpp
93 lines (74 loc) · 1.71 KB
/
intersection_2array.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
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
/* C++ PROGRAM TO FIND INTERSECTION OF 2 SETS*/
#include <iostream>
using namespace std;
void intersectionset(int a[], int b[], int n, int m);
int main()
{
int x[10000], y[10000];
int n, m;
cout << "ENTER NUMBER OF ELEMENTS IN A AND B RESPECTIVELY" << endl;
cin >> n >> m;
if (n == 0 || m == 0)
{
cout << "Intersection is NULL set" << endl;
exit(1);
}
cout << "Enter Elements of set A" << endl;
// TAKING INPUT OF ELEMENTS PRESENT IN SET A AND B RESPECTIVELY
for (int i = 0; i < n; i++)
{
cin >> x[i];
}
cout << "Enter Elements of set B" << endl;
for (int i = 0; i < m; i++)
{
cin >> y[i];
}
cout << "Intersection of 2 sets is" << endl;
intersectionset(x, y, n, m);
return 0;
}
void intersectionset(int a[], int b[], int n, int m)
{
int c[100000], x = 0, temp;
for (int i = 0; i < n; i++) // SORTING OF SET A IN DESCENDING ORDER
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < m; i++) // SORTING OF SET B IN DESCENDING ORDER
{
for (int j = i + 1; j < m; j++)
{
if (b[i] < b[j])
{
temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
for (int i = 0; i < n; i++) // COMPARING EACH ELEMENT OF SET A WITH ELEMENTS IN SET B , IF MATCH IS FOUND THEN STORING THAT ELEMENT IN ANOTHER SET W
{
for (int j = 0; j < m; j++)
{
if (a[i] == b[j])
{
c[x] = a[i];
x++;
}
}
}
for (int i = 0; i < x; i++)
{
if (c[i] != c[i + 1]) // THIS CONDITION REMOVES THE DUPLICATE ELEMENT PRESENT IN SET W AND PRINTS THE INTERSECTION OF 2 SETS A AND B
cout << c[i] << endl;
}
}