loading

Single Number II — Step-by-Step Visualization

mediumLeetCode #137ArrayBit Manipulation

Every element appears 3 times except one which appears once. Find it.

Algorithm Pattern

Bit Count mod 3

Key Idea

Count each bit position across all numbers. Bit set in single number appears 3k+1 times.

Step-by-Step Approach

  1. For each bit position 0..31
  2. Sum that bit across all numbers
  3. If sum % 3 != 0, set that bit in result

Related Problems