4

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)
aylr
  • 359
  • 1
  • 9
tim
  • 43
  • 1
  • 1
  • 4
  • 1
    Welcome to SO. Unfortunately this isn't a discussion forum or tutorial. Please take the time to read [ask] and the other links on that page. Invest some time with [the Tutorial](https://docs.python.org/3/tutorial/index.html) practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. You haven't asked a question. – wwii Mar 07 '18 at 14:26
  • Oh I shared my code but it's wrong, I just wanna know how it's wrong and I shared the question too at first. – tim Mar 07 '18 at 14:28
  • can you kindly add a full answer so I can test then accept it if it works for me, thanks. – tim Mar 07 '18 at 14:32
  • Possible duplicate of [Simple python leetCode not accepted on technicality?](https://stackoverflow.com/questions/49071897/simple-python-leetcode-not-accepted-on-technicality) – quamrana Mar 07 '18 at 14:42

4 Answers4

19

It is fairly easy, once you realize you have to work from the end of the list, so that your deletions do not change the part of the list you have not yet examined.

a = [1,1,1,3,4,5,5,5,5,7,7,7,9,9]
for i in range(len(a)-1,0,-1):
    if a[i] == a[i-1]:
        del a[i]

Result is

[1, 3, 4, 5, 7, 9]
BoarGules
  • 16,440
  • 2
  • 27
  • 44
1

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]
0

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.

briba
  • 2,857
  • 2
  • 31
  • 59
0

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