I used set() in Python but it didn't work


  • 0
    B

    I tried set() function and to my surprise it didn't work here. I know it is a little cheating here but it definitely gives you the right answer. Can anyone tell me why set() doesn't work for this question. Thanks much.


  • 0
    B

    I also encountered this kind of situation.
    Then I see the requirement: 'Your function should return length = 2, and A is now [1,2].'
    After use set() function, the A is also [1, 1, 2], it doesn't match the requirement.

    Then I tried the code below to modify A after use set() function. And it works.

    class Solution:
        # @param a list of integers
        # @return an integer
        def removeDuplicates(self, A):
            new = sorted(list(set(A)))
            length = len(new)
            for i in xrange(length):
                A[i] = new[i]
            return length
    

    PS: use A = sorted(list(set(A))) also doesn't work.


  • 0
    B

    Great. Thank you.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.