C Program to Delete the duplicate elements from an array

This page has a C Program to Delete the duplicate elements from an array. It is clear that, this C program will delete repeated (duplicated) elements from array. For example: If we enter 10 elements one by one 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 from an array. Kindly check out the program to Delete the duplicate elements from an array

Program to Delete duplicate elements from array

#include<stdio.h>

int main(){

int n, ar1[10], ar2[10], i = 0, j, k, t;

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

scanf("%d",&n);

printf("

Enter %d elements one by one :

", n);

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

scanf("%d",&ar1[j]);

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

{

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

{

if(ar1[j]==ar1[k])

break;

}

if(k==i)

{

ar2[i] = ar1[j];

++i;

}

}

printf("

The elements After removing Duplicates in Ascending Order are :

");

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

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

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

{ t=ar2[j];

ar2[j] = ar2[k];

ar2[k] =t;

}

printf("%d

",ar2[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: