INTERVIEW QUESTIONS
C
ARRAYS IN C
DETAILS
Question: How to remove duplicate elements from an array
Answer: #include<conio.h> #include<stdio.h> #include<string.h> void main() { //abcd are occurs two times in an array f[30] char f[30]={"abcdabcd"},name[30]; int i,j,m,n; clrscr(); m=strlen(f); n=m; for(i=0;i<n;i++) for(j=i+1;j<m;j++) if(f[i]==f[j]) { f[j]='$'; } j=0; printf("name=%s",f); for(i=0;i<m;i++) { if(f[i]!='$') { name[j]=f[i]; j++; } }name[j]='
|
Question:
How to remove duplicate elements from an array
Answer:
#include<conio.h> #include<stdio.h> #include<string.h> void main() { //abcd are occurs two times in an array f[30] char f[30]={"abcdabcd"},name[30]; int i,j,m,n; clrscr(); m=strlen(f); n=m; for(i=0;i<n;i++) for(j=i+1;j<m;j++) if(f[i]==f[j]) { f[j]='$'; } j=0; printf("name=%s",f); for(i=0;i<m;i++) { if(f[i]!='$') { name[j]=f[i]; j++; } }name[j]=' Source: CoolInterview.com
int main() { //2 and 3 are repeating int a[10] = {1,2,3,2,4,5,6,3,2,3}; int i,j; for(i=0;i<9;i++) { for(j=i+1;j<10;j++) { if(a[i] == a[j]) a[j] = 0;//Make the duplicate as 0; } } Source: CoolInterview.com
Answered by: vikas kumar shakya | Date: 12/4/2008
| Contact vikas kumar shakya
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|