I'm working on a method which removes duplicates of an element in an ArrayList, recursively. But I'm running into a bit of a problem, my method works and removes some elements, but not all of the duplicates.
Here's my the input:
100, 200, 200, 300, 400, 300, 100, 500, 500, 400, 100, 400, 100, 100
And here's the output:
100, 200, 300, 400, 100, 500, 100
And my method:
public static void removeDuplicates(ArrayList<Integer> list, int counter){
if(list == null){
throw new NullPointerException();
}
if(counter < list.size()){
if(list.contains(list.get(counter))){
list.remove(list.lastIndexOf(list.get(counter)));
}
removeDuplicates(list, ++counter);
}
}
I understand that I'm only removing the last element of said value, and then iterating to the next one. I was wondering how I should change this to remove all elements that are duplicates. Also, one part of my output that confuses me is, there are three values of '400', yet only one shows up in the output.
Thanks for the help.