Simple Python O(n) solution


  • -4
    B
    class Solution(object):
        def findDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            dict1 = {}
            for number in nums:
                if number not in dict1:
                    dict1[number] = 1
                else:
                    dict1[number] = dict1[number] + 1
            for keys in dict1:
                if dict1[keys] > 1:
                    return keys

  • 1
    D

    But your space usage is not O(1)


  • -1
    W
        class Solution(object):
            def findDuplicate(self, nums):
                """
                :type nums: List[int]
                :rtype: int
                """
                dict1={}
                for x in nums
    
    :
                if x in dict1:
                    return x
                else:
                    dict1[x]=1

Log in to reply
 

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