CodeSpeek

Single Number

Easy · Bit Manipulation

You are given an array of integers where every value appears exactly twice, except for one value that appears only once. Find and return that single value. Your solution should aim to use minimal extra space and a single pass if possible.

Examples

Input:  nums = [2,2,1]
Output: 1
Why:    2 appears twice, 1 appears once, so 1 is the answer.
Input:  nums = [4,1,2,1,2]
Output: 4
Why:    1 and 2 each appear twice, leaving 4 as the single value.
Input:  nums = [1]
Output: 1
Why:    With only one element, that element is the unique one.

Constraints

1 <= nums.length <= 3 * 10^4, -3 * 10^4 <= nums[i] <= 3 * 10^4, exactly one element appears once and all others appear exactly twice

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Single Number

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Single Number. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.