5. Single Number
easyAsked at RevolutFind the one element that appears once when every other appears twice, a Revolut warm-up that mirrors spotting an unmatched payment leg in a duplicated batch.
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 it with linear runtime and constant extra space.
Constraints
1 <= nums.length <= 3 * 10^4Every element appears twice except one
Examples
Example 1
nums=[2,2,1]1Example 2
nums=[4,1,2,1,2]4Approaches
1. Hash count
Count each value and return the one with count one.
- 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
XOR all elements; pairs cancel and the unique value remains. O(1) extra space.
- Time
- O(n)
- Space
- O(1)
function singleNumber(nums){
let acc = 0;
for (const x of nums) acc ^= x;
return acc;
}Tradeoff:
Revolut-specific tips
Revolut likes you to connect XOR to reconciliation parity bits — call out that this trick only works because debits and credits are matched as equal integer pairs.
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 Revolut interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →