My O(n) java solution getting TLE

Can you explain why this is giving TLE?

public class Solution {
public int removeDuplicates(ArrayList<Integer> a) {
    int n = a.size();
    int count=1;
    int i;
    for(i=0;i<n-1;i++){
        if(a.get(i+1) >a.get(i)){
            count++;
        }
        else{
            a.remove(i+1);
            i--;
            n--;
        }
    }
    return count;
    
}

}

Why TLE in O(n)?
public class Solution {
public int removeDuplicates(ArrayList A) {
if(A.size() <=2)
return A.size();
int p = 0;
int q = 1;
while(q<A.size()){
if(A.get§.equals(A.get(q))){
A.remove(q);
}else{
p++;
q++;
}
}
return A.size();
}
}

because remove() operation takes O(n) time so your code now turns to O(n ^ 2).
note: remove operation is proportional to the number of elements towards the right side of the current element.

The same thing is discussed many times below in the discussion list. It’s good to keep a check before posting the same questions.

Click here to start solving coding interview questions