About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Monday, September 2, 2013

C Program to Delete duplicate elements from an array

#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,k,n;
clrscr();

printf("Enter array size : ");
scanf("%d",&n);

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

clrscr();

printf("Original array is : ");
for(i=0;i<n;i++)
 printf(" %d",a[i]);

printf("Updated array is  : ");
for(i=0;i<n;i++)
{
   for(j=i+1;j<n;)
   {
      if(a[j]==a[i])
      {
         for(k=j;k<n;k++)
             a[k]=a[k+1];
          n--;
      }
      else
         j++;
   }
}

for(i=0;i<n;i++)
    printf("%d ",a[i]);
getch();
}

Output :
Enter array size : 5
Accept Numbers : 1 2 2 3 4
Original array is : 1 2 2 3 4
Updated array is  : 1 2 3 4

No comments: