Leetcode: Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.


Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

## Blog link: https://code.dennyzhang.com/remove-duplicates-from-sorted-array
class Solution(object):
    def removeDuplicates(self, nums):
        :type nums: List[int]
        :rtype: int
        if len(nums) == 0:
            return 0

        ret = 0
        for i in range(0, len(nums)-1):
            if nums[i] != nums[i+1]:
                ret += 1
                nums[ret] = nums[i+1]
            i += 1
        return (ret + 1)

Share It, If You Like It.

Leave a Reply

Your email address will not be published.