14. Single Number
easyAsked at ActivisionFind the element that appears once when every other element appears twice — Activision uses this to gauge bit-manipulation fluency before anti-cheat telemetry dedup questions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a non-empty array where every element appears twice except one, find that single element. Your algorithm should run in linear time and use only constant extra space.
Constraints
1 <= nums.length <= 3 * 10^4Every element appears twice except one-3*10^4 <= nums[i] <= 3*10^4
Examples
Example 1
nums=[2,2,1]1Example 2
nums=[4,1,2,1,2]4Approaches
1. Hash count
Count occurrences; return the key with count 1.
- Time
- O(n)
- Space
- O(n)
const c = new Map();
for (const n of nums) c.set(n, (c.get(n)||0)+1);
for (const [k,v] of c) if (v===1) return k;Tradeoff:
2. XOR accumulator
a XOR a = 0, a XOR 0 = a; folding XOR across the array leaves the lonely element. O(1) space.
- Time
- O(n)
- Space
- O(1)
function singleNumber(nums) {
let acc = 0;
for (const n of nums) acc ^= n;
return acc;
}Tradeoff:
Activision-specific tips
Activision favors candidates who reach for XOR here — the same identity-invariant trick reappears in anti-cheat telemetry dedup and packet-id reconciliation.
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 Activision interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →