---
# System prepended metadata

title: Leetcode 26 Array List Remove duplicates element
tags: [remove, LeetCode]

---

# Leetcode 26 Array List Remove duplicates element

My Solution (remove會比較慢)
---
```
class Solution(object):
    def removeDuplicates(self, nums):
        """刪除重複值"""
        last_index = len(nums) - 1
        pre,now = 0, 1 
        while (now <=last_index):
            if nums[pre] == nums[now]:
                nums.remove(nums[now]) # remove後 下一個點會上來
                last_index -= 1
            else:
                pre+=1
                now+=1
        print(nums)
        return len(nums)
```

Best Solution 
time:O(n) space:O(1)
---
```
def removeDuplicates(self, nums):
        if not nums: return 0
        i, j = 0, 1
        while j <= len(nums)-1:
        # 當 i 不等於 j 時  j可以取代 i+1 的下一個數
            if nums[i] != nums[j]:  
                i += 1 
                nums[i] = nums[j]  # 取代
            j += 1
        print(nums[:i+1])
        return i + 1
```

###### tags: `LeetCode` `remove`
