Write a function removedups() which takes an array A, its size n as parameters.The function removes the duplicate elements in A, and returns the new size of the array . Do not use an other arrays in this function.#include<stdio.h> int removedups(int a[], int i); int main() { int a[50],i,d,j,y; printf("entr the numbers respectively and enter ctrl-d in the end \n"); i=0; while(scanf("%d",&d)!=EOF) { a[i]=d; i=i+1; } y=removedups(a,i); printf("%d elements are unique among %d the new array is\n",y,i); for(j=0;j<y;j++) { printf("%d ",a[j]); } printf("\n"); return 0; } int removedups(int a[],int i ) { int count,j,temp,k,counter; count=0; for(j=0;j<i;j++) { k=j+1; counter=0; while(k<i) { if(a[j]==a[k]) { counter++; temp=a[j+counter]; a[j+counter]=a[k]; a[k]=temp; } k++; } if(counter==0) { a[count]=a[j]; count++; } j=j+counter; } return count; }
Removing duplicate from an Array
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment