← All problems

Single Number

easy

Given a non-empty array nums where every element appears exactly twice except for one, find that single element — in O(n) time and O(1) extra space.

Hint: XOR-ing a number with itself gives 0, and XOR is commutative — the duplicates cancel out.

Example

Input: nums = [4, 1, 2, 1, 2]
Output: 4
Loading...