Question Bank

Bit Manipulation Tricks

Difficulty: Medium

Mid-tier drills on XOR cancellation, mask construction, low-bit isolation, and bit counting. The patterns show up across hash-table tricks, DP states, and graph encodings.

Question Bank
/

Bit Manipulation Tricks

Bit Manipulation Tricks

Mid-tier drills on XOR cancellation, mask construction, low-bit isolation, and bit counting. The patterns show up across hash-table tricks, DP states, and graph encodings.

Question Bank
Medium
JavaScript
5 questions
bit-manipulation
xor-tricks
interview-prep
algorithms

747 views

15

Given an array where every value appears twice except one, return the lone value in O(n) time and O(1) space. Justify why XOR works.

Examples

Example 1:

Input: nums = [4, 1, 2, 1, 2]
Output: 4
Explanation: XOR is commutative, associative, and x ^ x = 0 with x ^ 0 = x. Paired values cancel pair-by-pair regardless of order. The remaining accumulator equals the single unpaired value.