Single Number II

Medium#137bit by bit

You get an array of integers in which every number occurs three times, except one that occurs once. Find that one in linear time and with O(1) extra memory. Our example: [3, 5, 3, 6, 5, 3, 5] — the answer is 6.

input
nums: an array of integers in which every number comes three times, except one that comes once.
returns
The number that comes once. It may be negative: a 32-bit answer built bit by bit has its sign in bit 31.
constraints
  • 1 ≤ nums.length ≤ 3001
  • −2³¹ ≤ nums[i] ≤ 2³¹ − 1
  • every number comes exactly three times, except one that comes once
our 14 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {number}
 */
function singleNumber(nums) {

}
Ctrl ↵
format
reset

saved in this browser

14 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more bit manipulation problems

Pattern “Bit manipulation” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.