Soohan-Kim / Leetcode

0 stars 0 forks source link

[HW 3] Remove Duplicates from Sorted Array #26. #30

Open Soohan-Kim opened 2 months ago

Soohan-Kim commented 2 months ago

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

Soohan-Kim commented 2 months ago
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        idx = 1

        if len(nums) == 1:
            return 1

        for i in range(1, len(nums)):
            if nums[i] != nums[i-1]:
                nums[idx] = nums[i]
                idx += 1
            elif i == len(nums)-1:
                nums[idx] = nums[i]

        return idx