Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Hash Maps
Math
Set Mismatch
Find the one duplicated number and the one missing number in an array that should contain the integers from 1 to n.
Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement
You are given an array of length containing numbers from $1n$. Exactly one number appears twice, and exactly one number is missing.
Return the duplicated number and the missing number.
Input Format
- An integer array
numsof lengthn. - Each value is intended to be in the range
1..n. - Exactly one value is duplicated and exactly one value is absent.
Output Format
Return an array/list of two integers [duplicate, missing].
Constraints
- The array contains integers in the range
1..n. - Exactly one number occurs twice.
- Exactly one number from
1..ndoes not appear.
Examples
Sample cases returned by the problem API.
Example 1
Input
nums = [1,2,2,4]
Output
[2,3]
Explanation
The number 2 appears twice, and 3 is missing from the range 1..4.
Example 2
Input
nums = [3,1,2,5,3]
Output
[3,4]
Explanation
The number 3 is duplicated, and 4 is missing.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.