Write a Function to store unique elements of one array into other array

Problem Statement
Write a function findunique () which takes an array A, its size n, and another array B as parameters. The
function stores in B the unique elements of A. It returns the size of B.



#include<stdio.h>
int findunique(int a[],int b[], int i);
int main()
{
  int a[50],b[50],i,d,j,y;
  printf("enter 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=findunique(a,b,i);
  printf("%d elements are unique among %d \n",y,i);
  for(j=0;j<y;j++)
    {
      printf("%d ",b[j]);
    }
  printf("\n");
  return 0;
}

int findunique(int a[],int b[], 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)
{
 b[count]=a[j];
 count++;
}
      j=j+counter;
    }    
  return count;
}
       

     

No comments:

Post a Comment