Results 1 to 2 of 2
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default removing duplicate numbers from an array

    I'm trying to remove duplicates from an arraylist of double values, but I can't get it to work right. At the moment it just deletes all the values rather than delete just the duplicates. Can anyone please see what i'm doing wrong here?

    Java Code:
        public static void removeDuplicates(ArrayList<Double> doubleArray)
        {
            ArrayList<Double> newList = new ArrayList<Double>();
            double delta = 0.001;
            for (Double d:doubleArray) {
                newList.add(d);
                int duplicate = 0;
                for (int i=newList.size()-1; i>=0; i--) {
                    //if any item in newList == d +- delta
                    if ((Math.abs(newList.get(i)/d)-1)<=delta) {
                        //increment duplicate (number of copies) & if there is 2 or more
                        if (++duplicate > 1) {
                            //remove index of duplicate
                            newList.remove(i);
                            //decrement number of copies
                            duplicate--;
                        }
                    }
                }
            }
            System.out.println("size 1: " + doubleArray.size());
            System.out.println("size 2: " + newList.size());
            doubleArray.clear();
            doubleArray.addAll(newList);
        }
    It is deleting everything in the list except the last item (because of the variable "duplicate"), but what I don't understand is that the IF statements should filter out non-duplicates but they're not.
    Last edited by ozzyman; 03-14-2011 at 09:16 PM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Okay i just noticed the mistake.......

    Java Code:
    if ((Math.abs(newList.get(i)/d)-1)<=delta) {

    the -1 should be in another bracket inside Math.abs(...) and now it works perfect :)
    sorry for the trouble

Similar Threads

  1. Array Help - Finding and replacing duplicate cells
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 02-05-2011, 06:06 PM
  2. How to detect duplicate values in an Array?
    By maz09 in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 08:58 AM
  3. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 09:58 AM
  4. removing duplicate whitespace
    By loki in forum New To Java
    Replies: 1
    Last Post: 04-25-2009, 06:54 PM
  5. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:33 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
  •