SDE & MLE Prep

136. Single Number

Problem

Every value in nums appears twice except one, which appears once. Return that one, in O(n) time and O(1) extra space.

Example: [4,1,2,1,2] → 4

XOR everything: pairs cancel, the single number remains.

Complexity: O(n) time, O(1) space.

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        return reduce(lambda a, b: a ^ b, nums)