Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0136 - Single Number
    0136 - Single Number
    0136 - Single Number
    0136 - Single Number

    0136 - Single Number

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/single-number/

    Solution 1 - XOR Bit Manipulation

    Given a non-empty array of integers where every element appears twice except for one, find the single element. The key insight is that XOR of a number with itself is 0, and XOR of a number with 0 is the number itself. By XORing all elements together, all pairs cancel out, leaving only the single number.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Using Python's reduce with XOR

    Same XOR approach but expressed functionally using reduce from the functools module. This applies the XOR operator cumulatively across all elements in a single expression.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0136.1 - Single Number - Solution 1 - XOR Bit Manipulation """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def singleNumber(self, nums: List[int]) -> int:
            """Single Number Function"""
            result = 0
            for num in nums:
                result ^= num
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().singleNumber([2, 2, 1]))         # 1
        print(Solution().singleNumber([4, 1, 2, 1, 2]))   # 4
        print(Solution().singleNumber([1]))                # 1
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0136.2 - Single Number - Solution 2 - Using Python's reduce with XOR """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    from functools import reduce
    from operator import xor
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def singleNumber(self, nums: List[int]) -> int:
            """Single Number Function"""
            return reduce(xor, nums)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().singleNumber([2, 2, 1]))         # 1
        print(Solution().singleNumber([4, 1, 2, 1, 2]))   # 4
        print(Solution().singleNumber([1]))                # 1
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()