Bit Manipulation XOR tricks

113. Single Number III

Medium

You are given an array of integers nums. Exactly two values in it appear only once. Every other value appears exactly twice. Return the two values that appear once.

You may return the two values in either order. The examples show them in ascending order, which is how the judge compares them.

Example 1

Input: nums = [3, 10, 5, 3, 6, 5]

Output: [6,10]

Explanation: 3 appears at indexes 0 and 3, and 5 at indexes 2 and 5. The values 6 and 10 appear once each.

Example 2

Input: nums = [7, 2]

Output: [2,7]

Explanation: Both values appear once. Returning [7,2] is accepted too.

Example 3

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

Output: [-2147483648,2147483647]

Explanation: 9 appears twice. The largest and the smallest 32-bit integers appear once each.

Check your understanding

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

nums = [4, 1, 4, 6]

Constraints

  • 2 ≤ nums.length ≤ 105
  • -231 ≤ nums[i] ≤ 231 − 1
  • Exactly two values appear once, and every other value appears exactly twice.

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.