You are here

C program to delete duplicate elements from an array

C program to delete duplicate elements from an array. For example, if an array contains following five elements: 1, 6, 2, 1, 9; in this array '1' occurs two times. After deleting duplicate element we get the following array: 1, 6, 2, 9.

C programming code

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int n, a[100], b[100], count = 0, c, d;
  6.  
  7.   printf("Enter number of elements in array\n");
  8.   scanf("%d", &n);
  9.  
  10.   printf("Enter %d integers\n", n);
  11.  
  12.   for (c = 0; c < n; c++)
  13.     scanf("%d", &a[c]);
  14.  
  15.   for (c = 0; c < n; c++)
  16.   {
  17.     for (d = 0; d < count; d++)
  18.     {
  19.       if(a[c] == b[d])
  20.         break;
  21.     }
  22.     if (d == count)
  23.     {
  24.       b[count] = a[c];
  25.       count++;
  26.     }
  27.   }
  28.  
  29.   printf("Array obtained after removing duplicate elements:\n");
  30.  
  31.   for (c = 0; c < count; c++)
  32.     printf("%d\n", b[c]);
  33.  
  34.   return 0;
  35. }

Output of program:
Enter number of elements in array
10
Enter 10 integers
1 5 1 2 3 9 2 5 8 6
Array obtained after removing duplicate elements:
1
5
2
3
9
8
6