Program to remove duplicate element in an array

Program to remove duplicate element in an array



Consider the following code in which we are removing duplicate element from an array:

CODE:

# include <stdio. h>

# include <conio. h>

void main ()

{

int a[20], i, j, k, n;

clrscr ();

printf (“\nEnter size of array : “);

scanf (“%d”, &n);

printf (“\nEnter %d array element : “, n);

for (i = 0; i < n; i ++)

{

scanf (“%d”, &a [i]);

}

printf (“\n Original array: “);

for (i = 0; i <  n; i ++)

{

printf (” %d”, a [i]);

}

printf (“\n New array: “);

for (i = 0; i < n; i ++)

{

for (j = i + 1; j < n; )

{

if (a [j] == a [i])

{

for (k = j; k < n; k ++)

{

a [k] = a [k + 1];

}

n –;

}

else {

j ++;

}

}

}

for(i = 0; i < n; i ++)

{

printf (“%d “, a [i]);

}

getch ();

}

OUTPUT:

Enter size of array: 4

Enter 4 element of array: 23 45 23 56

Original array: 23 45 23 56

New array: 23