Skip to main content

9. Single Number

easyAsked at Flipkart

Find the only element appearing once in an array where every other element appears twice — Flipkart uses this as a bit-manipulation gateway before harder dedup problems.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a non-empty array of integers where every element appears twice except one, find the unique element. Solve it in linear time and constant extra space.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • Each element appears twice except for one

Examples

Example 1

Input
nums = [2,2,1]
Output
1

Example 2

Input
nums = [4,1,2,1,2]
Output
4

Approaches

1. Hash map count

Count occurrences in a map, then scan for the value with count 1.

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 fold

XOR every element; duplicates cancel and the single value remains. O(1) extra memory.

Time
O(n)
Space
O(1)
function singleNumber(nums) {
  let r = 0;
  for (const x of nums) r ^= x;
  return r;
}

Tradeoff:

Flipkart-specific tips

Flipkart screeners reward candidates who explicitly call out the XOR identity (a ^ a = 0) and then connect it to real-world dedup of duplicate order events from their COD flow.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Single Number and other Flipkart interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →