6 Replies Latest reply: Nov 15, 2007 7:21 AM by 807600 RSS

    how to remove duplicate entries in int array ?

    807600
      how to remove duplicate entries in int array ?
        • 1. Re: how to remove duplicate entries in int array ?
          EJP
          Don't put them in there in the first place ...
          • 2. Re: how to remove duplicate entries in int array ?
            mlk
            AmitChalwade123456 wrote:
            how to remove duplicate entries in int array ?
            Take out a sheet of paper and have a go at it by hand.
            • 3. Re: how to remove duplicate entries in int array ?
              807600
              mlk wrote:
              AmitChalwade123456 wrote:
              how to remove duplicate entries in int array ?
              Take out a sheet of paper and have a go at it by hand.
              int[] arr = new int[4];
                        arr[0]=1001;
                        arr[1]=1002;
                        arr[2]=1001;
                        arr[3]=1003;
                        
                        for (int i = 0; i < arr.length; i++) {
                             for (int j =i; j < arr.length; j++){
                                  if ( j == i)
                                       continue;
                                  if (arr[i] == arr[j]) {
                                       System.out.println("match found");
                                       
                                  }
                             }
                        
                        }
              i did this it works fine but how do i remove that element from array
              • 4. Re: how to remove duplicate entries in int array ?
                mlk
                AmitChalwade123456 wrote:
                i did this it works fine but how do i remove that element from array
                The size of an array is fixed. You will have to create a new array at the new size. Copy the data. Set the variable to point at the new array.
                • 5. Re: how to remove duplicate entries in int array ?
                  EJP
                  BTW I wasn't joking in reply #1. Your example is artificial as it supplies the duplicate values in the initializer. In practice this is senseless: in the real world the simplest technique is not to insert the duplicates in the first place, by searching to see if they're already there first. Then you don't have the resize problem at all, or rather you only have the one you started with when inserting into the array.
                  • 6. Re: how to remove duplicate entries in int array ?
                    807600
                    I would suggest you look at the Set interface and what it does...

                    The collections utility classes are designed to prevent the pain of interacting with Arrays.

                    If you absolutely must have an array, then maybe add elements to a set and then convert back to array when you need it.

                    Something like


                    Set set = new HashSet<String> ;
                    set.add("one");
                    set.add("one");
                    set.add("two");
                    Object[] array = set.toArray()


                    *code not tested...

                    HTH

                    Edited by: huginn on Nov 15, 2007 5:20 AM