Adsense 728 * 15

Search

Custom Search

Write a c program to delete duplicate element in an array


c program to delete duplicate element in an array

//

#include<stdio.h>

void main()
{
int i,j,k;

int arr[] = {2,1,3,5,3,1,2};

int n = sizeof(arr)/sizeof(arr[0]);

// Logic of duplicate

for(i=0;i<n;i++)
{
   for(j=i+1;j<n;)
   {   
      if(arr[j]==arr[i])
      {   
         for(k=j;k<n;k++)
           /* Remove the index of duplicate element */       
             arr[k]=arr[k+1];
          n--;
      }   
      else
         j++;
   }   
}

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

}

No comments:

Post a Comment