10. Single Number
easyAsked at GrabFind the lone element in an array where every other element appears twice — Grab uses this to test bitwise fluency.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a non-empty array of integers where every element appears twice except for one, find the lone element. Solve in linear time and constant space.
Constraints
1 <= nums.length <= 3 * 10^4Each 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 occurrences then return the key with count 1.
- Time
- O(n)
- Space
- O(n)
const counts = new Map();
for (const n of nums) counts.set(n, (counts.get(n) || 0) + 1);
for (const [k, v] of counts) if (v === 1) return k;Tradeoff:
2. XOR fold
XOR cancels duplicates; folding XOR across the array leaves the unique element.
- Time
- O(n)
- Space
- O(1)
function singleNumber(nums) {
let x = 0;
for (const n of nums) x ^= n;
return x;
}Tradeoff:
Grab-specific tips
Grab interviewers reward candidates who reach the XOR insight without hints — frame the multi-service wallet as deduplicating transactions.
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 Grab interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →