C Program to Delete Duplicate elements from array

C Program to Delete Duplicate elements from array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

main()
{
   int arr[50],i,j,k,size;

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

   printf("\nEnter elements in Array : ");
   for(i=0;i<size;i++)
    scanf("%d",&arr[i]);

   printf("\nArray without duplicate elements : ");
   for(i=0;i<size;i++)
    {
      for(j=i+1;j<size;)
      {
         if(arr[j] == arr[i])
         {
            for(k=j;k<size;k++)
            {
               arr[k]=arr[k+1];
            }
            size--;
         }
         else
         j++;
      }
   }

   for(i=0;i<size;i++)
    {
      printf("%d ",arr[i]);
    }
}

OUTPUT:

Enter size of array : 5

Enter elements in Array : 1 2 3 1 7

Array without duplicate elements : 1 2 3 7

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.