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?
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.