5. Single Number
easyAsked at RappiFind the one element that appears once when every other element appears twice — Rappi frames this as finding the odd-out courier ID in a paired-shift roster.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a non-empty integer array where every element appears twice except one, find the single one. Your algorithm should run in linear time and use constant extra space.
Constraints
1 <= nums.length <= 3 * 10^4Each element except one appears twice
Examples
Example 1
nums = [2,2,1]1Example 2
nums = [4,1,2,1,2]4Approaches
1. Hash map count
Count occurrences and return the key with count 1.
- Time
- O(n)
- Space
- O(n)
const cnt = new Map();
for (const n of nums) cnt.set(n, (cnt.get(n)||0)+1);
for (const [k,v] of cnt) if (v === 1) return k;Tradeoff:
2. XOR accumulator
XOR cancels duplicate pairs to zero, leaving the unique value — constant space, one pass.
- Time
- O(n)
- Space
- O(1)
function singleNumber(nums) {
let x = 0;
for (const n of nums) x ^= n;
return x;
}Tradeoff:
Rappi-specific tips
Rappi loves the XOR trick because it mirrors how their dispatch ledger reconciles paired pickup/drop-off events in constant memory.
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 Rappi interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →