Saturday, October 19, 2013

Remove Duplicate Entries in an Array

Program:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[20],i,j,k,n;
clrscr();
printf("\nEnter Array size : ");
scanf("%d",&n);
printf("\nEnter Numbers :\n ");
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
clrscr();
printf("\nOriginal Array is : \n");
for(i=0;i<n;i++)
  printf("\n %d",a[i]);
printf("\nUpdated Array is  :\n ");
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("\n%d ",a[i]);
getch();
return 0;
}

Output:
Enter Array size : 5
Enter Numbers:
2
4
5
5
6
Original Array is:
2
4
5
5
6
Updated Array is:
2
4
5
6

No comments: