Skip to main content

18. Single Number

easyAsked at Plaid

Find the number that appears exactly once in an array where every other number appears twice. Plaid asks this because finding the un-matched ledger entry in a reconciliation pass has exactly this shape.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Blind (2026)Plaid intro — framed as reconciliation lookup.
  • LeetCode Discuss (2026)Plaid OA warm-up.

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 appears twice except for one element which appears 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 counting

Count occurrences in a hash map, return the one 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 [n, c] of count) if (c === 1) return n;
}

Tradeoff: Violates the O(1) space constraint. Mention only as the obvious starting point.

2. XOR everything

XOR is commutative and self-cancelling: a ^ a == 0. XOR all numbers; duplicates cancel out, leaving the single number.

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

Tradeoff: Bitwise trick that meets the constraint exactly. Demonstrates you reach for XOR for self-cancelling pair patterns.

Plaid-specific tips

Plaid uses this to verify bitwise-XOR fluency before harder reconciliation problems. Bonus signal: connect it to two-feed reconciliation — XOR the hashes of transactions from feed A with hashes from feed B, and the surviving bits identify the un-paired transactions.

Common mistakes

  • Initializing r to nums[0] then starting from i=1 — works but bigger surface area for off-by-ones than starting r=0.
  • Using addition instead of XOR — overflows and doesn't cancel.
  • Trying to sort first — that's O(n log n), missing the linear requirement.

Follow-up questions

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

  • Every element appears three times except one (LC 137) — harder, needs bit-by-bit counting mod 3.
  • Two elements appear once, rest twice (LC 260).
  • Reconcile two ledgers to find unpaired entries.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why does XOR work?

XOR is associative and commutative, and a ^ a = 0. So XORing the whole array reduces to the single element XOR'd with 0, which is itself.

Does this work with negative numbers?

Yes — JS does bitwise XOR on 32-bit signed ints. Within the LC bound, that's safe.

Practice these live with InterviewChamp.AI

Drill Single Number and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →