Single Number III

medium

Find two elements appearing once when others appear twice

Single Number III

Key Insight

XOR all gives a^b. Find a differing bit to split array into two groups.

Step 1Problem
No binary data

[1,2,1,3,2,5] - find two singles.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Single Number III

XOR all gives a^b. Find a differing bit to split array into two groups.

  1. Problem

    [1,2,1,3,2,5] - find two singles.

  2. XOR All

    XOR everything = 3 ^ 5 = 6 (0110)

  3. Find Differing Bit

    Bit 1 is set. Use it to split array.

  4. Split & XOR

    Group A (bit1=0): 1,1,5→5. Group B (bit1=1): 2,2,3→3.

  5. Result

    The two singles are 3 and 5!