Friday 22 November 2013

C PROGRAM TO DELETE DUPLICATE ELEMENTS IN THE ARRAY

#include<stdio.h>
#include<conio.h>

int main()
{

    int a[20],b[20];
    int n=0;
    int i=0,j=0,k=0;
    int flag=0;

    printf("enter the length of the array:");
    scanf("%d",&n);

    printf("\n\narray elements:");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    /*for(i=0;i<n-1;i++)
    {
        for(k=0;k<n-1;k++)
        {
            if(a[i]==a[k])
            {
                for(j=k;j<n-1;j++)
                {
                    a[j]=a[j+1];
                    n=n-1; //redusing length
                }
            }
        }
    }*/

    b[0]=a[0];
    k=1;

    for(i=1;i<n;i++)
    {
        for(j=0;j<k;j++)
        {
            if(a[i]==b[j])
            {
                flag=1;
                break;
        }
    }

     if(flag==0)
        {
            b[k]=a[i];
            k=k+1;
        }

     flag=0;
    }

    for(i=0;i<k;i++)
        printf("b[%d]=%d\n",i,b[i]);

    getch();
    return 0;
}

No comments:

Post a Comment