Set Mismatch

Updated on 07 July, 2025
Set Mismatch header image

Problem Statement

In this challenge, we begin with a set of integers s which should ideally contain all sequential numbers from 1 to n. A mistake has been made such that one number within this set is erroneously duplicated over another number, leading to one number appearing twice while another is entirely missing.

Given an integer array nums that represents the erroneous set, the task is to identify both the number that has been duplicated (appears twice) and the number that is missing from the set. The result should be returned as an array comprised of these two integers, with the first element being the duplicated number and the second being the missing number.

Examples

Example 1

Input:

nums = [1,2,2,4]

Output:

[2,3]

Example 2

Input:

nums = [1,1]

Output:

[1,2]

Constraints

  • 2 <= nums.length <= 104
  • 1 <= nums[i] <= 104

Approach and Intuition

To solve this problem, we must determine two key elements:

  1. The number that appears twice in the array.
  2. The number that is missing from the set.

To efficiently find these values, we must consider the properties of the numbers from 1 to n:

  1. Calculate Expected Sum: Calculate the sum of the first n natural numbers using the formula n * (n + 1) / 2. This gives us what the sum should be if there were no errors.
  2. Sum of Given Array: Compute the sum of the elements in the provided nums array.
  3. Track Appearances: As we track the sum, also keep a record of each number's occurrence to identify any number that appears twice.
  4. Find the Duplicated and Missing Numbers:
    • The number that appears twice will be directly identified during the scanning of the array based on its frequency.
    • The missing number can be computed by considering the difference between the expected sum and the actual sum of the unique numbers from the array. Adjust this difference by accounting for the repeated number to find the missing one.

For example, in a scenario where nums = [1, 2, 2, 4]:

  • Calculate the expected sum for n = 4, which is 10.
  • The sum of the array nums is 9.
  • From tracking, we see that the number 2 occurs twice.
  • Knowing 2 is duplicated, we can calculate the missing number. The missing number should adjust the sum to match the expected sum which leads us to discover that 3 is missing.

The detail of this method allows us to efficiently identify both the duplicated and missing numbers, satisfying the constraints and requirements of the problem.

Solutions

  • Java
java
public class Solution {
    public int[] findDuplicatesAndMissing(int[] elements) {
        int combinedXOR = 0, separateXOR1 = 0, separateXOR2 = 0;
        for (int value: elements)
            combinedXOR ^= value;
        for (int i = 1; i <= elements.length; i++)
            combinedXOR ^= i;
        int uniqueBit = combinedXOR & ~(combinedXOR - 1);
        for (int value: elements) {
            if ((value & uniqueBit) != 0)
                separateXOR1 ^= value;
            else
                separateXOR2 ^= value;
        }
        for (int i = 1; i <= elements.length; i++) {
            if ((i & uniqueBit) != 0)
                separateXOR1 ^= i;
            else
                separateXOR2 ^= i;
        }
        for (int i = 0; i < elements.length; i++) {
            if (elements[i] == separateXOR2)
                return new int[]{separateXOR2, separateXOR1};
        }
        return new int[]{separateXOR1, separateXOR2};
    }
}

The Set Mismatch problem involves identifying a duplicate number and a missing number from a continuous sequence represented within an array. The solution uses XOR operations to efficiently determine the two numbers.

Here's a breakdown of how the implementation in Java approaches the problem:

  • Initialize three XOR variables (combinedXOR, separateXOR1, and separateXOR2) to process the entire elements array and a sequence from 1 to n. The variable combinedXOR accumulates the XOR of both the array and the range from 1 to n, effectively isolating the difference between the duplicate and the missing number due to their unique occurrence.
  • Compute a uniqueBit, which is a bit position that can differentiate between the two desired numbers. This bit is determined by isolating the rightmost set bit of combinedXOR.
  • Use uniqueBit to separate the numbers into two groups. Each number from the array and the range 1 to n is checked against this uniqueBit, and XORed into separateXOR1 or separateXOR2 accordingly. This separates the effects of the duplicate and missing numbers.
  • Identify which of the two XOR results (separateXOR1 and separateXOR2) is the duplicate. This is done by scanning through the original array.
  • The scan effectively checks which number between separateXOR1 and separateXOR2 appears twice within the array.
  • Return the duplicate and missing numbers as the solution.

Ensure to utilize this approach to identify misplaced or repeated elements in sequences efficiently with a time complexity of O(n) and space complexity of O(1), leveraging bitwise operations. This solution is particularly useful in scenarios where minimal extra space usage is crucial.

Comments

No comments yet.