18. Single Number
easyAsked at SalesforceFind the element that appears only once when every other element appears twice. Salesforce uses this to test the XOR trick and the recognition that O(1) space is achievable.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses this as a litmus test for bit-manipulation awareness.
- Blind (2025-10)— Recurring on Salesforce backend phone screens.
Problem
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space.
Constraints
1 <= nums.length <= 3 * 10^4-3 * 10^4 <= nums[i] <= 3 * 10^4Each element in the array appears twice except for one element which appears once.
Examples
Example 1
nums = [2,2,1]1Example 2
nums = [4,1,2,1,2]4Example 3
nums = [1]1Approaches
1. Hash count
Build a frequency map; return the key with count 1.
- Time
- O(n)
- Space
- O(n)
function singleNumber(nums) {
const count = new Map();
for (const n of nums) count.set(n, (count.get(n) || 0) + 1);
for (const [k, v] of count) if (v === 1) return k;
}Tradeoff: Linear time but violates the O(1) space requirement. Salesforce will explicitly fail this.
2. XOR fold
XOR all elements together. Pairs cancel (a XOR a = 0); the single element remains.
- Time
- O(n)
- Space
- O(1)
function singleNumber(nums) {
let result = 0;
for (const n of nums) result ^= n;
return result;
}Tradeoff: Three lines. Exploits the property that XOR is commutative and self-inverse. Salesforce wants this in 30 seconds.
Salesforce-specific tips
Salesforce flags this as a 'do you know your bitwise tricks' filter. They specifically want you to articulate the two properties (commutative + self-inverse) that make XOR work. Bonus signal: mention that this generalizes to 'every element appears k times except one' for some k (using mod-k counters per bit).
Common mistakes
- Using ^ but storing in a non-number (e.g., string) — silent type coercion.
- Initializing result to a non-zero value — gives result XORed with that value.
- Falling back to the hash map because XOR feels 'tricky' — Salesforce expects the optimal answer.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Single Number II (LC 137) — every other element appears three times.
- Single Number III (LC 260) — two elements appear once.
- Missing Number (LC 268) — XOR with 0..n.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does XOR work here?
Because XOR is commutative (order doesn't matter) and self-inverse (a XOR a = 0). Every pair cancels itself, leaving only the unpaired element.
Could I sort first?
Sorting + comparing adjacent pairs works but is O(n log n), violating the linear-time requirement.
Practice these live with InterviewChamp.AI
Drill Single Number and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →