Here is a C++ program to print unique elements in an array
SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
int a[20][2],num;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array : ";
for(int i=0;i<num;i++)
{
cin>>a[i][0];
a[i][1]=0;
}
for(int i=0;i<num;i++)
{
if(a[i][1]==1)
continue;
for(int y=i+1;y<num;y++)
if(a[y][0]==a[i][0])
{
a[i][1]=1;
a[y][1]=1;
}
}
cout<<"Elements that appears only once : ";
for(int i=0;i<num;i++)
if(a[i][1]==0)
cout<<a[i][0]<<" ";
return 0;
}
using namespace std;
int main()
{
int a[20][2],num;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array : ";
for(int i=0;i<num;i++)
{
cin>>a[i][0];
a[i][1]=0;
}
for(int i=0;i<num;i++)
{
if(a[i][1]==1)
continue;
for(int y=i+1;y<num;y++)
if(a[y][0]==a[i][0])
{
a[i][1]=1;
a[y][1]=1;
}
}
cout<<"Elements that appears only once : ";
for(int i=0;i<num;i++)
if(a[i][1]==0)
cout<<a[i][0]<<" ";
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment