Java O(n) solution and Time Limit Exceeded


I don’t understand why there is this problem, since the complexity is O (n).

public int removeDuplicates(ArrayList a) {
Integer lastValue = -1;
Iterator i = a.iterator();
Integer currentValue =;
if(currentValue.equals( lastValue) ){
lastValue = currentValue;
return a.size();


because removing from ArrayList is itself O(n) so your solution becomes O(n2)