‹ DS&A interview · Socratic
DSA · Bit Manipulation · #165

Single Number

Module 50 · difficulty 2/5·30:00starts on first keystroke

Given a non-empty array of integers `nums`, every element appears **twice** except for one element which appears **once**. Find that single element. Implement the function `singleNumber(nums)` returning the integer that appears exactly once. You must implement a solution with linear runtime complexity and use only constant extra space.

Examples
  • nums = [2,2,1] 11 appears once; 2 appears twice.
  • nums = [4,1,2,1,2] 4Only 4 is unpaired.
  • nums = [1] 1A single element with no pair.
Constraints
  • · 1 <= nums.length <= 3 * 10^4
  • · -3 * 10^4 <= nums[i] <= 3 * 10^4
  • · Each element appears exactly twice except for one element which appears exactly once.
  • · The array length is always odd.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.