Bit Manipulation XOR tricks

110. Single Number

Easy 147 DSA Sheet

You are given an array of integers nums. Every value in it appears exactly twice, except one value, which appears only once. Return the value that appears once.

The two copies of a value can be anywhere in the array. They do not have to be next to each other.

Example 1

Input: nums = [4, -9, 13, 4, 0, 13, 0]

Output: -9

Explanation: 4 appears at indexes 0 and 3, 13 at indexes 2 and 5, and 0 at indexes 4 and 6. Only -9 appears once.

Example 2

Input: nums = [6, 6, 0]

Output: 0

Explanation: 6 appears twice, so the answer is 0.

Example 3

Input: nums = [-2147483648, 2147483647, -2147483648]

Output: 2147483647

Explanation: The smallest 32-bit integer appears at both ends of the array. The largest one appears once, between them.

Check your understanding

Work out the answer for this input before you write any code.

nums = [10, -10, 10]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -231 ≤ nums[i] ≤ 231 − 1
  • Every value appears exactly twice, except one value, which appears once.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.