Leetcode: Single Number

Single Number



Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## Blog link: https://code.dennyzhang.com/single-number
class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        x = 0
        for i in nums:
            x = x ^ i
        return x

if __name__ == '__main__':
    s = Solution()
    print s.singleNumber([1, 2, 1])
    print s.singleNumber([2, 2, 1])
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.