Solution by arun42

  • 0

    Using Bitwise Operator XOR
    We know XOR of two numbers (a and b) a^b equals zero if both the numbers are equal.
    So the idea is two find the xor of all numbers in an array , numbers which appear twice cancels out while XORing and number which appears once will remain .

    Code in Python

    class Solution(object):
        def singleNumber(self, nums):
            return reduce(lambda x,y:x^y ,nums)

    Time Complexity : O(n)
    Space Complexity : O(1)

Log in to reply

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