C Program to delete duplicate elements in an array
//C code for removing repeated values of an array
#include<stdio.h>
int main()
{
int a[20],i,j,k,n;
printf("\nnEnter array size : ");
scanf("%d",&n);
printf("\nnAccept Numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nnOriginal array is : ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\nnUpdated array is : ");
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 array will contain 0 in deleted duplicate value
}
else
j++;
}
}
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\n");
}
/*
array size : 5
Numbers : 22
33
44
44
22
array is : 22 33 44 44 22
array is : 22 33 44
*/
No comments:
Post a Comment