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

The full question, and I'm starting to learn python online but having issue with this question marked as easy

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.

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        k=0
        for i in range(i+1,len(nums)-1):
            j=i+1

            for j in range(j+1,len(len(nums))):
                if nums[i] == nums[j]:
                    del nums[j]
        len_list = nums
        return(len_list, nums)


Solution 1:[1]

Looks like you are solving a LeetCode question: https://leetcode.com/problems/remove-duplicates-from-sorted-array/

If yes, this should be your answer:

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return 1

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

        return i + 1

It doesn't matter what you leave beyond the returned length.

Solution 2:[2]

First answer from @BoarGules is the best one. But we can do it in forward direction also.

a = [1,1,1,3,4,5,5,5,5,7,7,7,9,9]
i =0
l = len(a)-1
while i < l :
    if a[i] == a[i+1]:
        del a[i+1]
        l -=1
    else :
        i +=1    
print(a)

Result will be :

[1, 3, 4, 5, 7, 9]

Solution 3:[3]

below code remove duplicates & sort array in descending order.

class Solution(object):
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a_value=sorted(set(nums),reverse=True)
        if len(a_value) > 2:
           a=a_value[2]
        else:
           a=a_value[0]
        return a

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 briba
Solution 2 Hamza Sahib
Solution 3