""" 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()