InferenceAI

Enter at least two characters to search.

Start free

Two Lonely Numbers

mediumBit Manipulation25 min25 XP

In nums, exactly two values appear once and all others appear twice. Return the two values in ascending order.

Requirements

  • O(n)O(n) time and O(1)O(1) space with XOR.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[1,2,1,3,2,5]]
TASKtwo_lonely_numbers()
EXPECTED OUTPUT[3,5]

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [1,2,1,3,2,5]

Output: [3,5]

Explanation: classic

Input: nums = [-1,0]

Output: [-1,0]

Explanation: negative

Input: nums = [0,1]

Output: [0,1]

Explanation: two values

Hint 1

XOR everything to get a ^ b.

Hint 2

Any set bit of a ^ b splits the numbers into two groups, each containing one lonely number.

Topics

DSABit ManipulationPython