forked from sachin-nono/codingpractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoArrays.cpp
More file actions
66 lines (48 loc) · 1.69 KB
/
IntersectionOfTwoArrays.cpp
File metadata and controls
66 lines (48 loc) · 1.69 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
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Intersection Of Two Arrays
Check which integer presents in both array and then print them.
*/
#include<iostream>
using namespace std;
int main()
{
int N, M, arr1[20], arr2[20], i, j, k, l;
cout<<"Enter array size : ";
cin>>N;
M=N;
cout<<"\nEnter first array elements :\n";
for(i=0; i<N; ++i)
cin>>arr1[i];
cout<<"\nEnter second array elements :\n";
for(i=0; i<N; ++i)
cin>>arr2[i];
for(i=0; i<N; ++i)
{
int a=arr1[i];
int x=1;
for(j=0; j<M; ++j) //searching both the arrays contains same element or not
if(arr2[j]==a) //if found,
{
int y=1;
for(int k=i+1; k<N; ++k) //counting in first array number of times same element appeared
if(arr1[k]==a)
++x;
for(int k=j+1; k<M; ++k) //counting in second array number of times same element appeared
if(arr2[k]==a)
++y;
int min=x<y?x:y; //which integer presents in both the arrays
for(int k=0; k<min; ++k)
cout<<a<<" ";
for(int k=i+1; k<N; ++k)
if(arr1[k]==a) //deleting same elements in first array
{
for(int l=k; l<N; ++l)
arr1[l]=arr1[l+1];
--N;
--l;
}
break;
}
}
return 0;
}