My solution in O(n) using negation technique.


  • 0
    G
        def findDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            result = -9999999
            
            for num in nums:
                if nums[abs(num)-1] < 0:
                    result = abs(num)
                else:
                    nums[abs(num) - 1] *= -1
                
            return result ```

Log in to reply
 

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