Bit toggling - python


  • -1
    Y
    def findDuplicate(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            #toggle bits if num is found in array
            bits = 0 
            for n in nums: 
                nthbit = (bits >> n) & 1
                if nthbit == 0:
                    bits |= 1 << n
                else:
                    return n
            return -1

Log in to reply
 

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