Python solution with O(n) time and O(1) space using bit manipulation


  • 0
    H
    def findDuplicate(self, nums):
        bitmask = 0
        for n in nums:
            if bitmask & (1 << (n - 1)):
                return n
            bitmask |= (1 << (n - 1))    
        return -1
    

Log in to reply
 

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