10. Single Number
easyAsked at MonzoFind the one transaction ID that appears exactly once in a list where every other ID appears twice.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a non-empty array of integers nums where every element appears twice except for one, find that single one. You must implement a solution with linear runtime complexity and use only constant extra space.
Constraints
1 <= nums.length <= 3 * 10^4Each element appears twice except for one which appears once
Examples
Example 1
nums = [2,2,1]1Example 2
nums = [4,1,2,1,2]4Approaches
1. Brute force
Count occurrences with a hash map and return the key with count 1.
- Time
- O(n)
- Space
- O(n)
const m = new Map();
for (const x of nums) m.set(x, (m.get(x) || 0) + 1);
for (const [k, v] of m) if (v === 1) return k;Tradeoff:
2. XOR fold
XOR all elements; duplicates cancel and the lone value remains. Constant space and one linear pass.
- Time
- O(n)
- Space
- O(1)
function singleNumber(nums) {
let acc = 0;
for (const x of nums) acc ^= x;
return acc;
}Tradeoff:
Monzo-specific tips
Monzo values O(1)-space tricks that translate cleanly to ledger reconciliation passes over large transaction batches.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Single Number and other Monzo interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →