c program for removing the duplicate element in an array

				
					#include<stdio.h> 
#include<conio.h>
#include<stdlib.h> 
void main() 
{ 
int *ar, i, j, n, y, t;
clrscr(); 
printf(“Enter the number of elements in the array”); 
scanf(“%d”, &n); 
ar=(int*) malloc(sizeof(int)*n); 
for(i=0; i<n; i++) 
{ 
printf(“Enter a number”); 
scanf(“%d”, &ar[i]); 
} 
for(i=0; i<n; i++) 
{ 
for(j=i+1; j<n; j++) 
{ 
if(ar[i]>ar[j]) 
{ 
t=arr[i]; 
ar[i]=ar[j]; 
ar[j]=t; 
} 
} 
} 
printf(“Elements of array after sorting”); 
for(i=0; i<n; i++) 
printf(“%d”, ar[i]); 
i=0;
j=1;
while(i<n) 
{ 
if(ar[i]==ar[j]) 
{ 
for(y=j; y<n-1; y++)
ar[y]=ar[y+1];
n--;
} 
else
{
i++; 
j++;
} 
} 
printf(“Elements of array after removing duplicate elements”); 
for(i=0; i<=n; i++) 
printf(“%d”, ar[i]); 
getch(); 
}
				
			
Output: 

Enter the number of elements in the array:5
Enter a number: 3
Enter a number: 3
Enter a number: 4
Enter a number: 6
Enter a number: 4

Elements of array after sorting:
3
3
4
4
6

Elements of array after removing duplicate elements:
3
4
6

Leave a Reply