Python 1 line O(n) solution 44ms 95%

  • 4
    class Solution(object):
        def containsDuplicate(self, nums):
            return len(nums) > len(set(nums))

  • 0

    My more verbose solution does the same thing but somehow is slower. Can you explain why?

    class Solution( object ):
        def containsDuplicate(self,nums):
            unique = set(nums)
            if len(unique) == len(nums):
                return False
            return True

  • 0

    They are of the same complexity level except that your code results in a little bit more machine code. I doubt whether this tiny difference can results in notable runtime difference.
    I guess if you submit your version a few more times, you can see 44ms as well.

Log in to reply

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