Program to Remove duplicate Element in an Array

Program to Remove duplicate Element in 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. int main()
  3. {
  4.   int n, a[100], b[100], count = 0, c, d;
  5.   printf(“Enter number of elements in array\n”);
  6.   scanf(“%d”, &n);
  7.   printf(“Enter %d integers\n”, n);
  8.   for (c = 0; c < n; c++)
  9.     scanf(“%d”, &a[c]);
  10.   for (c = 0; c < n; c++)
  11.   {
  12.     for (d = 0; d < count; d++)
  13.     {
  14.       if(a[c] == b[d])
  15.         break;
  16.     }
  17.     if (d == count)
  18.     {
  19.       b[count] = a[c];
  20.       count++;
  21.     }
  22.   }
  23.   printf(“Array obtained after removing duplicate elements:\n”);
  24.   for (c = 0; c < count; c++)
  25.     printf(“%d\n”, b[c]);
  26.   return 0;
  27. }

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