DEV Community

Discussion on: Remove Duplicates from Sorted Array - Leetcode

Collapse
 
salehmdev profile image
Mohamed Saleh • Edited

Nice write-up. Good idea to use 2 pointers to improve the time complexity while using constant space.

However, in the code the pop(j) has a worst time complexity of O(n) since all the elements after that index have to shift to the left (making the collective worst time complexity O(n^2)).

One trick get around this is to swap j and the last element of the list and then do a pop() which should reduce the time of the pop to be O(1) since it's removing the last element of the list.