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 program

#include <stdio.h>

int main()
{
  int n, a[100], b[100], count = 0, c, d;

  printf("Enter number of elements in array\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &a[c]);

  for (c = 0; c < n; c++)
  {
    for (d = 0; d < count; d++)
    {
      if(a[c] == b[d])
        break;
    }
    if (d == count)
    {
      b[count] = a[c];
      count++;
    }
  }

  printf("Array obtained after removing duplicate elements:\n");

  for (c = 0; c < count; c++)
    printf("%d\n", b[c]);

  return 0;
}

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