Skip to main content

18. Single Number

easyAsked at Snowflake

Short answer

  • Best approach: XOR all elements. x XOR x = 0 and x XOR 0 = x. XOR-ing every element cancels duplicates, leaving only the single.
  • Time O(n), space O(1)
  • How often Snowflake asks it: frequently asked · Verified

Find the one number in an array where every other appears twice. Snowflake asks this because the XOR trick is exactly the same primitive used to construct erasure-coded data: cheap, commutative, parallel-friendly.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)— Snowflake storage team uses this to set up an erasure-coding follow-up.
  • LeetCode Discuss (2025-11)— Recurring at Snowflake new-grad screens.

Problem

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4
  • Each element in the array appears twice except for one element which appears only once.

Examples

Example 1

Input
nums = [2,2,1]
Output
1

Example 2

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

Approaches

1. Hash map counts

Count occurrences; return the key with count 1.

Time
O(n)
Space
O(n)
function singleNumber(nums) {
  const count = new Map();
  for (const n of nums) count.set(n, (count.get(n) || 0) + 1);
  for (const [k, v] of count) if (v === 1) return k;
}

Tradeoff: Violates the O(1) space requirement. Mention only to acknowledge.

2. XOR all elements (optimal)

x XOR x = 0 and x XOR 0 = x. XOR-ing every element cancels duplicates, leaving only the single.

Time
O(n)
Space
O(1)
function singleNumber(nums) {
  let acc = 0;
  for (const n of nums) acc ^= n;
  return acc;
}

Tradeoff: O(1) space, parallelizable (XOR is commutative + associative), perfect for SIMD or partitioned reduction.

Snowflake-specific tips

Snowflake interviewers love this one because the XOR insight is the same primitive used in RAID and erasure coding — both rely on XOR's self-inverse property. Bonus signal: discuss how this aggregates across partitions: each compute node XORs its slice, then a reduce step XORs the per-partition results — no central coordinator needed.

Common mistakes

  • Using sum and subtracting expected duplicates — only works if you know all values up front.
  • Sorting first — O(n log n), violates the linear constraint.
  • Worrying about overflow with XOR — there isn't any.

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Single Number II (LC 137) — every other appears three times.
  • Single Number III (LC 260) — two singles among duplicates.
  • How does erasure coding use XOR?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why does XOR work here?

XOR is commutative and associative, so order doesn't matter. x XOR x = 0, and 0 XOR x = x. So pairs cancel out and only the single survives.

How does Snowflake use XOR in storage?

Reed-Solomon / erasure coding uses XOR-like operations over a finite field. The same self-inverse property lets you reconstruct a lost data block from the others plus a parity block.

Originally published on InterviewChamp.AI

Companies that also ask Single Number