Results 1 to 8 of 8
  1. #1
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Removing Duplicates from Array

    Hi i was trying remove duplicates element from my array without using collection API but i didn't got any output from my code.Although it is compiled successfully but on execution it didn't give any output. Please help.Really appreciate it.
    I guess there must be some problem in function Duplicate
    Java Code:
    class Union
    {
    
    public static void main(String...s)
    
    {
    Union M=new Union();
    int x[]=new int[]{1,0,1,4,10,10,10,3,567,4,3,33};
    int y[]=new int[]{5,4,5,4,5,4,2,3,3,1,0};
    int []w=M.merge(x,y);
    int []z=M.sort(w);
    int []v=M.Duplicate(z);
    for(int i=0;i<v.length;i++)
    {
    if(v[i]!='0')
    System.out.println(v[i]);
    }
    
    }
    
    int[] merge(int x[],int y[])
    {
    int B[]=new int[x.length+y.length];
    for(int i=0;i<B.length;i++)
    B[i]='0';
    
    int p=0;int r=0;int z=0;int q=x.length-1;int s=y.length-1;
    while(p<=q) {
    B[z++]=x[p++];
    
    }
    while(r<=s) {
    B[z++]=y[r++];
    
    }
    return B;
    
    }
    
    int[] sort(int x[])
    {int temp;
    for(int i=0;i<x.length;i++)
    {
    for(int j=1;j<(x.length)-i;j++)
    {
    if(x[j]<x[j-1])
    {
    temp=x[j];
    x[j]=x[j-1];
    x[j-1]=temp;
    }
    }
    }
    return x;
    }
    
    
    int[] Duplicate(int w[])
    {
    for(int i=0;i<w.length;i++)
    {
    for(int j=0;j<w.length;j++)
    {
    if(w[i]==w[j])
    {
    w[j]='0';
    }
    }
    }
    return (w);
    }
    
    
    }
    Last edited by iamsethi; 12-18-2014 at 05:45 AM.

  2. #2
    iamsethi is offline Member
    Join Date
    Dec 2014
    Posts
    19
    Rep Power
    0

    Default Re: Removing Duplicates from Array

    Got the result!! i was using :

    for(int i=0;i<w.length;i++)
    {
    for(int j=0;j<w.length;j++) in the function Duplicate (of course a[i]==a[j] every time here so this code was removing all the elements)

    instead of for(int i=0;i<w.length;i++)
    {
    for(int j=i+1;j<w.length;j++)
    Last edited by iamsethi; 12-18-2014 at 05:56 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Removing Duplicates from Array

    So you consider an element removed if its value is '0'? So a value of 48 is not considered a value? You better thoroughly document it ...

    kind regards,

    Jos

    ps. nice code indentation and nice variable names ...
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Removing Duplicates from Array

    Its a special kind of code written specifically so you forget how it works in exactly 24 hours.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Removing Duplicates from Array

    Quote Originally Posted by gimbal2 View Post
    ... you forget how it works in exactly 24 hours.
    As long as that?

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Removing Duplicates from Array

    Well it depends on if you were breast-fed or not as a baby.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Removing Duplicates from Array

    Wow... This discussion has gone off the rails fast!

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Removing Duplicates from Array

    Welcome to the forum!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Removing Duplicates in an Array
    By jerseyjava in forum New To Java
    Replies: 7
    Last Post: 02-01-2012, 07:46 PM
  2. Remove duplicates in 2D array
    By lakshmibvaraprasad in forum New To Java
    Replies: 5
    Last Post: 07-22-2011, 08:49 PM
  3. Removing duplicates from double Arrays
    By jhong253 in forum New To Java
    Replies: 3
    Last Post: 10-30-2010, 10:32 PM
  4. Removing Duplicates.
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 01:03 PM
  5. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •