Single Number

easy · bit-manipulation, xor, array

Single Number

Given an integer array nums, every element appears twice except for one. Return the element that appears only once.

Function signature

func SingleNumber(nums []int) int

Example

nums = [2,2,1]
output = 1

Constraints

  • 1 <= len(nums) <= 200000
  • -1_000_000_000 <= nums[i] <= 1_000_000_000

Notes

  • Aim for O(n) time and O(1) extra space.
  • XOR cancels paired values.
Run tests to see results
No issues detected
    Join Discord