find-duplicate.py (382B)
1 class Solution: 2 3 # negate the index for each number 4 # if we find an index has already been 5 # negated, this is duplicate. 6 7 def findDuplicate(self, nums: List[int]) -> int: 8 9 for num in nums: 10 index = abs(num) - 1 11 if nums[index] < 0: 12 return index + 1 13 else: 14 nums[index] *= -1 15 16 return -1