Hello. I trying to solve this problem. The user inputs 4 numbers to each array (they are 2). I must output the common elements. So this is the problem i'm stucking with .
Let's say
p = 1,2,2,3
p1 = 3,5,1,2
the output is 2 1 3 1 2
but it must be 2 1 3
How can i remove the duplicates?
here is the code
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main()
{
int p[4] ,p1[4], num = 0 , p2[] = {};
cout<<"Enter 4 numbers of the first array ";
for(int a=0;a<4;a++){
cin>>p[a];
}
system("CLS");
cout<<"Enter 4 numbers of the second array: ";
for(int a=0;a<4;a++){
cin>>p1[a];
}
system("CLS");
for(int i = 0 ; i < 4; ++i) {
for (int j = 0 ; j < 4; ++j) {
if(p[i]==p1[j]) {
p2[num++] = p[j];
}
}
}
cout<<"The number of common elements"<<num<<endl;
cout<<"These are the common numbers: ";
for (int k=0;k<num;k++){
cout<<p2[k]<<" ";
}
getch();
return 0;
}