Search⌘ K
AI Features

Single Number II

Explore how to solve the problem of finding two numbers that appear only once in an array where all others appear twice. Learn to apply bitwise manipulation techniques efficiently while using constant extra space to build optimal solutions.

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:

Go
usercode > main.go
package main
func twoSingleNumbers(arr []int) []int {
// Replace this placeholder return statement with your code
return make([]int, 0)
}
Single Number II