C program to delete duplicate elements in an array

This page has a c program to delete duplicate elements in an array. It is clear that, this C program will delete repeated (duplicated) elements using single array. For example: If we enter 10 elements one by one to an array as 51 35 48 24 55 -43 48 35 24 29, then the program displays the elements, after removing Duplicates (in Ascending Order) are -43 24 29 35 48 51 55. The below given C program will Delete the duplicate elements using single array. Kindly check out the program to Delete the duplicate elements in an array

Program to delete duplicate elements in an array

#include<stdio.h>

int main() {

int i, j, k, t, max, ar[30];

printf(" How many elements to the Array : ");

scanf("%d", &max);

printf("

Enter %d elements one by one :

", max);

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

scanf("%d", &ar[i]);

printf("



The elements Before removing Duplicates are :

");

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

printf("%d ", ar[i]);

for (i = 0; i < max; ++i){

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

if (ar[j] == ar[i]) {

for (k = j; k < max; ++k) {

ar[k] = ar[k + 1];

}

--max;

}

else

j++;

}

printf("



The elements After removing Duplicates in Ascending Order are :

");

for(j=0;j<max;j++){

for(k=j+1;k<max;k++)

if(ar[j]>ar[k])

{ t=ar[j];

ar[j] = ar[k];

ar[k] =t;

}

printf("%d

",ar[j]);

}

return (0);

} Sample Output: How many elements to the Array : 10

Enter 10 elements one by one :

51 35 48 24 55 -43 48 35 24 29

The elements After removing Duplicates in Ascending Order are

-43

24

29

35

48

51

55

Related Popular Posts: