Asked at 12 companies
5 recent
- Bloomberg
- Amazon
- Microsoft
- Meta
- Amdocs
- tcs
- Huawei
- Airbnb
- Qualcomm
+2 moreShow fewer
- Cognizant
- Adobe
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · File permissions: Unix stores read, write and execute as three bits, so chmod 755 is just bits: 7 = 111 (rwx), 5 = 101 (r-x).
ProblemProblem statementGiven a non-empty array of integers, every element appears twice except for one. Find that single one. The solution must have linear runtime complexity and use only constant extra space.ReadHide
Problem Description
Given a non-empty array of integers, every element appears twice except for one. Find that single one. The solution must have linear runtime complexity and use only constant extra space.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Utilize the XOR property A ^ A = 0 and A ^ 0 = A. XORing all elements in the array will cancel out all paired numbers, leaving only the unique single number.
Approaches
1. XOR Property (Optimal) - O(N) Time, O(1) Space ⭐
Idea: The mathematical property of the XOR operator is perfect for this:
$$ A \oplus B \oplus A = (A \oplus A) \oplus B = 0 \oplus B = B $$
By XORing every element in the array together, all pairs cancel out, leaving only the single, unpaired number.
2. Brute Force - O(N²)
Idea: For every element, iterate through the rest of the array to check for a duplicate.
Solution code
import sys
from typing import List
from functools import reduce
import operator
class Solution:
# Optimized: O(N) Time, O(1) Space - XOR Application
def singleNumber_optimal(self, nums: List[int]) -> int:
# XORing all elements together
# A ^ A = 0, A ^ 0 = A. Pairs cancel out.
return reduce(operator.xor, nums)
# Brute Force: O(N) Time, O(N) Space - Hash Set (Fails O(1) space constraint)
def singleNumber_brute(self, nums: List[int]) -> int:
seen = set()
for num in nums:
if num in seen:
seen.remove(num)
else:
seen.add(num)
# The remaining element is the single number
return seen.pop()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language