Thursday, 26 June 2014

C++ program to remove duplicates elements in an array


Here is a C++ program to remove duplicate elements in an array


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
    int a1[20],num,redup[20],k=0,flag=0;
    cout<<"How many elements to be stored (max 20) : ";
    cin>>num;
    cout<<"Enter elements of array a1 : ";
    for(int i=0;i<num;i++)
        cin>>a1[i];
    for(int i=0;i<num;i++)
    {
       for(int y=i+1;y<num;y++)
            if(a1[i]==a1[y])
                 flag=1;
       if(flag==0)
           redup[k++]=a1[i];
       flag=0;
    }
    cout<<"After removing duplicates : ";
    for(int i=0;i<k;i++)
        cout<<redup[i]<<" ";
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment