Why my code throws TLE?


#1

Why is it so that my code(below) throws TLE and the solution(also below) passes all test case without TLE? If anyone can explain…

My Code:

    int i=0;
    while(i<a.size()-1){
      if(Math.abs(a.get(i)-a.get(i+1))<0.00000001){
          a.remove(i+1);     
      }else i++;
    }
    return a.size(); 

The Solution Code which passes without TLE:

    if(a.size()<0){
        return 0;
    }

    for(int i=a.size()-1;i>=1;i--)
    {
        if((int)a.get(i)==(int)a.get(i-1))
        {
            a.remove(i);
        }
    }
    
    return a.size();