Skip to content

Problem

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

Example 1:

**Input:** nums = [2,2,1]
**Output:** 1

Solve

  • Using binary xor (^ operation in python), we can nullify any two time appear number
  • The remaining is the needed answer
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        a_number = 0
        for n in nums:
            a_number ^= n

        return a_number

Last update : September 17, 2023
Created : August 16, 2023