Delete Duplicate Elements in Array


Prev Tutorial Next Tutorial

Delete Duplicate Elements in an Array in C

Array is collection of similar data type, In this code we enter only Integer type values and remove all duplicate value form array list. To delete all duplicate elements from array list you need looping and if else statement.

delete duplicate array element

C Program to Remove Duplicate Elements in an Array

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

void main()
{
 int arr[20], i, j, k, size;
 
   printf("\nEnter Size of an Array : ");
   scanf("%d", &size);
 
   printf("\nEnter any %d Numbers : ",size);
   for (i = 0; i < size; i++)
      scanf("%d", &arr[i]);
 
   printf("\nArray with Unique list  : ");
   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]);
   }
getch();
}

Output

Enter Size of an Array : 5
Enter any 5 Numbers : 3 1 4 6 4
Array with Unique list  : 3 1 4 6 

Prev Tutorial Next Tutorial

Advertisements
Download Projects