Python solution use set()


  • 0
    J
    class Solution(object):
        def containsDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            path = set()
            for element in nums:
                if element in path:
                    return True
                else:
                    path.add(element)
            return False
    

    Later, I realized that it can be simplified as,

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

Log in to reply
 

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