How to remove duplicate elements from an array

Questions by mitu218

Showing Answers 1 - 27 of 27 Answers

sreelu

  • Mar 11th, 2006
 

tha tperticular element in array makes zeroeg: int a[3]={2,3,2} a[0]=0;

  Was this answer useful?  Yes

Richa Yadav

  • Mar 12th, 2006
 

Code
  1. void rem_dup(int *arr, int size)

  2. {      

  3. int i, j;

  4. j=1;   

  5. for(i=1;

  6. i<size;

  7. i++)

  8. {

  9. if(arr[i]!=arr[j-1])

  10. {

  11. arr[j]=arr[i];

  12. j++;

  13. }

  14. else

  15. j++;

  16. }

  17. }

  18.  

marimuthu

  • Mar 12th, 2006
 

hello friends just run this pgm get the output

its form by string array

i am doing final cse plz reply

#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]='\0';

   printf("\nduplicate eliminated name=%s",name);

   getch();


}

  Was this answer useful?  Yes

Given prog can remove all duplicate int values. It can be further used for anything

int search1(int a[], int b[],int k)
{
        int i=0,j=0;
        for(i=k;i<10;i++)
        {
                while(b[j]!='\0' && j<10)
                {
                        if(a[i]==b[j])
                                return 0;
                        j++;
                }
        }
        return 1;
}
int
main()
{
        int a[10], b[10];
        int i=0,j=0;
        for(i=0;i<10;i++)
        {
                scanf("%d",&a[i]);
                b[i] = '\0';
        }

        for(i=0;i<10;i++)
        {
                if(search1(a,b,i))
                        b[j++] = a[i];
        }
        for(i=0;b[i]!='\0';i++)
                printf("\n %d ",b[i]);
return 0;
}

  Was this answer useful?  Yes

bala nagi reddy

  • Mar 22nd, 2006
 

Hi, There is a simple solution to this... Any solution to this is of O(N^2) but this solution takes just O(N) The solution is just xor up all the elements of the array one by one. i.e arr[0] XOR arr[1] and check if the value is zero. if its zero then the element just added is the duplicate element. This procedure can be used to find one duplicate element.same thing can be repeated by removing the first found duplicate elements and all the duplicate elements can be found. correct me if there is any prob with the solution.Thanks,Bala

  Was this answer useful?  Yes

yah Hi Bala

There is a problem with the sol

say we have 4 elements 4,5,6,5

then when we XOR 4&5 we'll get 001

and when we XOR result&6 we'll get 111

now with duplicate 5

result XOR 5 we'll get 010 which is non zero

then how can u say the sol is correct

plz clarify and corect me if m wrong

Thanks

Nitin

  Was this answer useful?  Yes

karunakar

  • Apr 14th, 2006
 

Hi,

       Your idea is good, But i think it may cause some other problem like..... if ZERO is present in the given input... is it works properly??

  Was this answer useful?  Yes

haripanda

  • Apr 17th, 2006
 

this is simplejust uwill apply the linear searchtechnique to find the duplicate

duplicateremove(int a[], int n)

{

int i,j,k;

for(i=0;i<n;i++)

{

for(j=i+1;j<n;j++)

{

if(a[i]==a[j])

{

printf("duplicate = %d",n);

for(k=j;k<n-1;k++)

a[k]=a[k+1];

a[n-1]=0;

n=n-1;//size of arryis now decresed

}

}

}

}

if any bug plz debug and repaly

  Was this answer useful?  Yes

Sreekanth P Krishnan

  • Apr 17th, 2006
 

 void main()

{

int a[20];

int i,j,k;

//read array

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

{

 if(a[i]==a[j])

   for(k=j;k<n-1;k++)

     a[k]=a[k+1];

}

}

  Was this answer useful?  Yes

shashikanth

  • Apr 20th, 2006
 

hi this is shashi from sreenidhi eng college

i looked upon ur answer its too long

to be done

i think a final year student should be a little short to the anser

so that we can save the time of ours

ok then i think u will look through it

hoping it

               bye then

meet u afterwards

  Was this answer useful?  Yes

asdf

  • Jul 31st, 2006
 

actually u can use binary search tree and remove duplicates in nlogn time

  Was this answer useful?  Yes

kernel32

  • May 25th, 2008
 

Hey guyz,
if we talk about the Order, then would it be better to first sort the array, which would take O(nlogn) and then remove duplicate, which again would take O(n). So total Complexity would be of O(nlogn).

  Was this answer useful?  Yes

rimi.mnnit

  • May 31st, 2008
 

//simple BST program with little change


#include
#include
struct btree
{
        int data;
        struct btree *lnode;
        struct btree *rnode;
};

typedef struct btree *tree_pntr;
typedef struct btree tree;
void maketree(tree_pntr *,int);
void inorder(tree_pntr);
int main()
{
    tree_pntr node;
    int no,i;
    node=NULL;
    int a[10]={1,3,6,3,8,1,4,8,3,9};
      for(i=0;i<10;i++)
      {
        maketree(&node,a[i]);
      }
    inorder(node);
    getch();
}

void maketree(tree_pntr *nd,int dt)   
{
    if(*nd==NULL)
    {
            *nd=malloc(sizeof(struct btree));     
           (*nd)->data=dt;
           (*nd)->lnode=NULL;
           (*nd)->rnode=NULL;
    }
    else
    {
        if((*nd)->data       maketree(&((*nd)->rnode),dt);
       if((*nd)->data>dt)
       maketree(&((*nd)->lnode),dt);          //NO case for same element
    }
}
void inorder(tree_pntr nd)
{
     if(nd)
     {
        inorder(nd->lnode);
        printf("node is=%d",nd->data);
        inorder(nd->rnode);
     }
}        
      

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions