import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
class RemoveDuplicates {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
list.add(1);
list.add(1);
list.add(4);
System.out.println(RemoveDuplicates.rmDuplicates(list));
}
public static List<Integer> rmDuplicates(List<Integer> list) {
Collections.sort(list);
Iterator<Integer> it = list.iterator();
int index = 0;
while (it.hasNext() && index < list.size() - 1) {
int current = it.next();
index++;
if (current == list.get(index)) {
it.remove();
index--;
}
}
return list;
}
}
Update: I did not want a sorted list. Just removing duplicates is enough and I wanted it in place. I thought without sorting it would be tricky to remove duplicates.