Set Mismatch - Problem

You're given an array nums that was supposed to contain all integers from 1 to n exactly once, but something went wrong! One number got duplicated, which means another number went missing.

Your mission: Find both the duplicate number (appears twice) and the missing number (doesn't appear at all).

Example: If nums = [1,2,2,4], then:

  • The duplicate is 2 (appears twice)
  • The missing number is 3 (should be there but isn't)
  • Return [2,3]

Goal: Return an array [duplicate, missing] where the first element is the number that appears twice, and the second is the number that's missing.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,2,4]
โ€บ Output: [2,3]
๐Ÿ’ก Note: The number 2 appears twice (duplicate) and the number 3 is missing from the sequence 1,2,3,4.
example_2.py โ€” Different Duplicate
$ Input: nums = [1,1]
โ€บ Output: [1,2]
๐Ÿ’ก Note: For array of length 2, we should have [1,2]. The number 1 is duplicated and 2 is missing.
example_3.py โ€” Larger Array
$ Input: nums = [3,2,3,4,6,5]
โ€บ Output: [3,1]
๐Ÿ’ก Note: Array should be [1,2,3,4,5,6]. The number 3 appears twice and 1 is missing.

Constraints

  • 2 โ‰ค nums.length โ‰ค 104
  • 1 โ‰ค nums[i] โ‰ค 104
  • nums contains n distinct numbers plus one duplicate
  • The array represents numbers from 1 to n with exactly one duplicate and one missing

Visualization

Tap to expand
๐ŸŽ“ The Missing Attendance MysteryExpected: Students 1, 2, 3, 4 | Actual: [1, 2, 2, 4]๐Ÿ“‹ Roll Call1โœ“ Present2โœ“โœ“ Twice!3โœ— Absent4โœ“ Present๐Ÿ” Detection ProcessStep 1: Build attendance setseen = {}Process 1: seen = {1}Process 2: seen = {1,2}Process 2: Already in set!โ†’ duplicate = 2Process 4: seen = {1,2,4}arraySum = 1+2+2+4 = 9๐Ÿ“Š Math MagicExpected sum formula:sum = nร—(n+1)/2sum = 4ร—5/2 = 10Missing number formula:missing = expectedSum - arraySum + duplicatemissing = 10 - 9 + 2 = 3๐ŸŽฏ SolutionDuplicate: 2Missing: 3[2, 3]O(n) timeO(n) space๐Ÿ’ก Key Insight:Since we know exactly what numbers should be present (1 to n), we can use the mathematicalrelationship between expected sum and actual sum to find the missing number efficiently!This combines hash table detection with arithmetic insight for optimal performance.
Understanding the Visualization
1
Take Attendance
Call out each student number and mark who responds
2
Notice Duplicate
Student #2 responds twice - suspicious!
3
Find Missing
Use math: expected total responses minus actual responses plus duplicate
4
Solve Mystery
Student #2 answered twice, Student #3 is absent
Key Takeaway
๐ŸŽฏ Key Insight: Combine hash table duplicate detection with mathematical sum relationships to solve both parts of the problem efficiently in one pass!
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.2K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen