educative.io

Why can't we pop from the array in-place?

I’m considering below solution:

    def remove_duplicates(arr):
      i = 0
      while i < len(arr)-1:
        if arr[i] == arr[i+1]:
          arr.pop(i)
        else:
          i += 1
      return len(arr)

Poping from an intermediate address is O(k) as the array must shift all values to the left afterwards. The provided solution has a better time complexity due to that. If you started from the last indexes and used pop from end, this would be a better solution.