Asked at 7 companies
6 recent
- Amazon
- Microsoft
- Bloomberg
- tcs
- Meta
- Criteo
Community-reported · source · updated 2026-10-08. ● 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
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementYou have a set of integers 1 to n. One number is duplicated (appears twice) and one number is missing. Return [duplicated, missing].ReadHide
Problem Statement
You have a set of integers 1 to n. One number is duplicated (appears twice) and one number is missing. Return [duplicated, missing].
Added 2026-11-01 from Srujan's LeetCode Recent AC list (photo) — not previously in the repo.
Input / Output
Input: nums = [1,2,2,4]
Output: [2,3]
Input: nums = [1,1]
Output: [1,2]
Input: nums = [2,2]
Output: [2,1]
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
Stuck? One hint at a time.
Approach
Pattern Recognition
Math / Set — the numbers are supposed to be exactly 1..n, so the expected sum is known: n(n+1)/2. The duplicate is the only value that appears twice, and the missing number is the gap between the expected sum and the real sum once the duplicate is removed.
Approach
Brute Force — Count Every Value, O(n²)
for x from 1 to n:
count how many times x appears in nums
if count == 2: duplicate = x
if count == 0: missing = x
return [duplicate, missing]
Optimized — Sum and Set, O(n)
seen = set(nums) # duplicate = the value that fails to grow the set
duplicate = the value seen twice
missing = n(n+1)/2 - (sum(nums) - duplicate)
return [duplicate, missing]
Why it works: sum(nums) - duplicate is the sum of the set with no repeats, so the expected sum minus that sum is exactly the missing number.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Count every value | O(n²) | O(1) | Re-scans the array per value |
| Sum and set | O(n) | O(n) | One pass, uses the known total |
Solution code
from typing import List
class SolutionBruteForce:
"""O(n^2) time | O(1) space — count every value 1..n by re-scanning."""
def findErrorNums(self, nums: List[int]) -> List[int]:
n = len(nums)
duplicate = missing = -1
for x in range(1, n + 1):
count = sum(1 for v in nums if v == x)
if count == 2:
duplicate = x
elif count == 0:
missing = x
return [duplicate, missing]
class Solution:
"""O(n) time | O(n) space — find the duplicate with a set, the missing with sums."""
def findErrorNums(self, nums: List[int]) -> List[int]:
n = len(nums)
seen = set()
duplicate = -1
for v in nums:
if v in seen:
duplicate = v
seen.add(v)
expected = n * (n + 1) // 2
missing = expected - (sum(nums) - duplicate)
return [duplicate, missing]Other ways to solve it
- 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