Search⌘ K
AI Features

Single Number II

Discover how to solve the problem of finding two elements that appear only once in an array where all others appear twice. This lesson guides you through implementing a bitwise manipulation solution that uses constant extra space and handles large integer constraints efficiently.

Statement

Given a non-empty array arr, in which exactly two elements appear once, and all the other elements appear twice, return the two elements that appeared only once.

Note: The result can be returned in any order. The solution should use only constant extra space.

Constraints:

  • 22 \leq arr.length 103\leq 10^3

  • 231−2^{31} \leq arr[i] 2311\leq 2^{31}-1

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Two Single Numbers

1.

(Select all that apply.) What is the output if the following array is given as an input?

arr = [1, 6, 2, 1, 2, 4, 7, 4] Multi-select

A.

[6, 2]

B.

[6, 7]

C.

[7, 6]

D.

[1, 2, 4]


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground:

JavaScript
usercode > main.js
export function twoSingleNumbers(arr){
// Replace this placeholder return statement with your code
return []
}
Single Number II