Search⌘ K
AI Features

Single Number II

Explore how to identify two unique numbers in an array where all other elements appear twice. Understand the bitwise manipulation techniques used to solve this problem efficiently with constant extra space, and practice implementing the solution to strengthen your coding interview skills.

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:

Java
usercode > TwoSingleNumbers.java
import java.util.*;
public class TwoSingleNumbers{
public static int[] twoSingleNumbers(int[] arr) {
// Replace this placeholder return statement with your code
return new int[]{};
}
}
Single Number II