CodeArchon

easyBit ManipulationFacebookAmazon

Lone Rune Finder

A monk sorts magical runes that always come in matching pairs — except one rogue rune. Find it.

Given a non-empty integer array where every element appears twice except one, return that single number.

Choose approach

4
0
i
1
1
2
2
1
3
2
4
Mapstate
x→4
bin→00000100
1/6
Narrationstep 1 of 6

XOR with 4 → x = 4 (bin 00000100).

1def singleNumber(nums):
2 x = 0
3 for n in nums: x ^= n
4 return x
Variables
i=0
x=4
bin=00000100

How to think about it

  1. XOR is commutative + associative.
  2. Loner survives because pairs annihilate.

Edge cases to catch in interview

  • Single element -> itself.
  • Negative numbers OK.

Examples

Example 11
{"values":[2,2,1]}

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed