c - Removing Duplicate values and adding 0 on their place -
i have written code remove duplicate values , add 0 on there place . feel code should better this,if can give better idea of developing code. please suggest me , advice me.
- input--2,3,4,3,6
- output--2,3,4,0,6
here code:
#include<stdio.h> int main() { int a[100],b[100]; int i,j,size; scanf("%d",&size); for(i=0;i<size;i++) { scanf("%d",&a[i]); } for(i=0;i<size;i++) { b[i]=a[i]; } for(i=0;i<size;i++) { for(j=i+1;j<size;j++) { if(a[i]==a[j]) { b[j]=0; } } } for(i=0;i<size;i++) printf("%d\n",b[i]); return 0; }
clear duplicates entered follows, comparing values entered far:
#include<stdio.h> int main() { int a[100]; int i,j,size; scanf("%d",&size); for(i=0;i<size;i++) { scanf("%d",&a[i]); for(j=0;j<i;j++){ if(a[j]==a[i]) { a[i]=0; /* found duplicate among previous entries! */ break; } } } for(i=0;i<size;i++) printf("%d\n",a[i]); return 0; }
Comments
Post a Comment