My accepted Python solution using simple math

  • -8
    class Solution(object):
    def findDuplicate(self, nums):
        :type nums: List[int]
        :rtype: int
        # for i in range(len(nums)):
        #     if nums[i] in nums[i+1:]:
        #         return nums[i]
        nums_uniq = list(set(nums))
        occurrence = len(nums) - len(nums_uniq)
        return int((sum(nums) - sum(nums_uniq)) / occurrence)

  • 0

    Since they've specifically asked that the space complexity be O(1), this does not satisfy all conditions (since you're creating a set & a new list).

Log in to reply

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