Bit Manipulation XOR tricks

112. Single Number II

Medium

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

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

Example 1

Input: nums = [6, 11, 4, 6, 4, 6, 4]

Output: 11

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

Example 2

Input: nums = [4, -1, 4, 4]

Output: -1

Example 3

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

Output: -2147483648

Explanation: The largest 32-bit integer appears three times. The smallest one appears once.

Check your understanding

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

nums = [3, 3, 8, 3]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -231 ≤ nums[i] ≤ 231 − 1
  • Every value appears exactly three times, 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.