InferenceAI

Enter at least two characters to search.

Start free

Lonely Number

easyBit Manipulation15 min10 XP

Every element of nums appears exactly twice except one. Return that element.

Requirements

  • O(n)O(n) time and O(1)O(1) extra space. XOR has the properties x⊕x=0x \oplus x = 0 and x⊕0=xx \oplus 0 = x.

Examples

EXAMPLE WALKTHROUGHsmall

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

INPUT[[2,2,1]]
TASKlonely_number()
EXPECTED OUTPUT1

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [2,2,1]

Output: 1

Explanation: small

Input: nums = [4,1,2,1,2]

Output: 4

Explanation: middle

Input: nums = [7]

Output: 7

Explanation: single

Hint 1

XOR all the values together; pairs cancel out.

Topics

DSABit ManipulationPython