Java O(n) solution and Time Limit Exceeded


#1

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();
while(i.hasNext()){
Integer currentValue = i.next();
if(currentValue.equals( lastValue) ){
i.remove();
}
lastValue = currentValue;
}
return a.size();
}


#2

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