Knowledge Guide
HomeDSACompany Practice

easy Set Mismatch

Problem Statement

You are given an array nums of size n, originally containing 1 to n integers. Unfortunately, due to some error, one number got duplicated, replacing another number that now goes missing from the sequence.

Identify the duplicated number and the missing number and return them as a pair.

Examples

Try it yourself

Try solving this question here:

✅ Solution Set Mismatch

Problem Statement

You are given an array nums of size n, originally containing 1 to n integers. Unfortunately, due to some error, one number got duplicated, replacing another number that now goes missing from the sequence.

Identify the duplicated number and the missing number and return them as a pair.

Examples

  • Example 1:

    • Input: nums = [1,3,3,4]
    • Expected Output: [3,2]
    • Justification: Here, 3 appears twice, and 2 is missing from the sequence.
  • Example 2:

    • Input: nums = [2,2,3,5,4]
    • Expected Output: [2,1]
    • Justification: 2 is duplicated, and 1 is missing, making 2 the repeated number and 1 the missing one.
  • Example 3:

    • Input: nums = [1,5,3,2,7,7,6]
    • Expected Output: [7,4]
    • Justification: In this sequence, 7 is the number that appears twice, and 4 is absent, indicating 7 as the duplicate and 4 as the missing number.

Solution

To solve this problem, we'll employ an approach that meticulously scans through the given array to identify both the duplicate and the missing numbers. This method is efficient because it doesn't require sorting or additional data structures that significantly increase complexity. The core idea revolves around using the properties of numbers and their indices in the array, thus ensuring a solution that is both space and time-efficient.

Our strategy includes iterating over the array and marking visited numbers by flipping the sign of the number at the index corresponding to each value encountered. This technique helps us detect the duplicate number when we stumble upon a value pointing to an index with an already negative number. For finding the missing number, we'll go through the array once more to find the first positive number, indicating the index of the missing number. This approach stands out for its direct manipulation of the array, reducing the need for extra space and ensuring a quick solution.

Step-by-Step Algorithm

  1. Initialize Variables: Start by initializing two variables, duplicate to -1 (to store the duplicated number) and missing to -1 to store missing number.

  2. Identify Duplicate: Loop through each number n in the input array.

    • Calculate the index i as the absolute value of n minus 1 (since arrays are 0-indexed).
    • If the value at index i in the array is negative, this means n is the duplicated number (as we have visited this index before). Set duplicate to n.
    • Otherwise, mark the value at index i as visited by multiplying it by -1.
  3. Find Missing Number: Loop through the array again to find the missing number.

    • For each index i, check if the value at i is positive.
    • If found positive, it means this index (representing i+1 number) was never visited, i.e., i+1 is the missing number. Set missing to i+1.
  4. Return Result: Return an array containing duplicate and missing as the first and second elements, respectively.

Algorithm Walkthrough

Let's consider the Input: [1,5,3,2,7,7,6]

  1. Initialize Variables:

    • duplicate = -1
    • missing = -1
  2. Identify Duplicate:

    • Start with the original array: [1, 5, 3, 2, 7, 7, 6]

    • Iteration 1: Process n = 1, marking index 0 (1-1=0). Updated array: [-1, 5, 3, 2, 7, 7, 6].

    • Iteration 2: Process n = 5, marking index 4 (5-1=4). Updated array: [-1, 5, 3, 2, -7, 7, 6].

    • Iteration 3: Process n = 3, marking index 2 (3-1=2). Updated array: [-1, 5, -3, 2, -7, 7, 6].

    • Iteration 4: Process n = 2, marking index 1 (2-1=1). Updated array: [-1, -5, -3, 2, -7, 7, 6].

    • Iteration 5: Process n = 7, marking index 6 (7-1=6). Updated array: [-1, -5, -3, 2, -7, 7, -6].

    • Iteration 6: Process n = 7 again, find that index 6 is already negative, thus identifying 7 as the duplicate. The array remains unchanged: [-1, -5, -3, 2, -7, 7, -6].

    • Iteration 7: Process n = 6, marking index 5 (6-1=5). updated array: [-1, -5, -3, 2, -7, -7, -6].

  3. Find Missing Number:

    • Revisiting the correctly updated array: [-1, -5, -3, 2, -7, -7, -6], we look for the first positive number.

    • The positive number is at index 3 (2), indicating that the number 4 (index+1) is missing from the original array.

  4. Return Result:

    • The algorithm identifies 7 as the duplicate and 4 as the missing number, so the result returned is [7, 4].
Image
Image

Code

java
import java.util.Arrays;

public class Solution {

  // Method to find the duplicated and missing number
  public int[] findErrorNums(int[] nums) {
    int duplicate = -1, missing = -1;
    // Mark the visited numbers by negating the values at their corresponding indexes
    for (int n : nums) {
      if (nums[Math.abs(n) - 1] < 0) duplicate = Math.abs(n);
      else nums[Math.abs(n) - 1] *= -1;
    }
    // Find the missing number by checking the positive values
    for (int i = 0; i < nums.length; i++) {
      if (nums[i] > 0) missing = i + 1;
    }
    // Return the results as an array
    return new int[] { duplicate, missing };
  }

  // Main method to test the examples
  public static void main(String[] args) {
    Solution solution = new Solution();
    System.out.println(
      Arrays.toString(solution.findErrorNums(new int[] { 1, 3, 3, 4 }))
    ); // Example 1
    System.out.println(
      Arrays.toString(solution.findErrorNums(new int[] { 2, 2, 3, 5, 4 }))
    ); // Example 2
    System.out.println(
      Arrays.toString(solution.findErrorNums(new int[] { 1, 5, 3, 2, 7, 7, 6 }))
    ); // Example 3
  }
}

Complexity Analysis

Time Complexity:

The algorithm iterates through the array of numbers twice. The first iteration is to identify the duplicate number by marking the indices visited (negative marking). The second iteration is to find the missing number by checking for a positive value in the modified array. Since each iteration goes through the array once, and there are two iterations, the time complexity is linear with respect to the size of the input array, n.

Space Complexity:

The solution modifies the input array in place to track visited numbers and does not use any additional data structures that grow with the input size. The space used for the output and a few variables does not depend on the input size, making the space complexity constant, .

🤖 Don't fully get this? Learn it with Claude

Stuck on Set Mismatch? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Set Mismatch** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Set Mismatch** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Set Mismatch**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Set Mismatch**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes