← back to blog

Set Mismatch

Two JavaScript solutions to LeetCode 645 using a Set and in-place sign marking.

Problem

LeetCode 645 · Easy. The array nums has length n and originally contained each integer from 1 through n exactly once. One number was replaced by another number already in the array. Return [duplicate, missing], in that order. Set Mismatch on LeetCode

Example 1
Input:  nums = [1, 2, 2, 4]
Output: [2, 3]

Example 2
Input:  nums = [1, 1]
Output: [1, 2]
  • 2 ≤ nums.length ≤ 10⁴
  • 1 ≤ nums[i] ≤ 10⁴. The problem also guarantees that each value comes from the original range 1…n.

My Solutions

Approach 1 · Track seen values with a Set

Scan nums and add each new value to a Set. If a value is already in the set, record it as the duplicate. Then check every candidate from 1 through n. The first candidate absent from the set is the missing number.

For [1, 2, 2, 4], the second 2 gives us the duplicate. The set ends up holding 1, 2, and 4, so the second pass finds 3 missing.

Animated walkthrough · Set

The set grows to {1, 2, 4}. The second 2 is the duplicate, and checking the candidates finds that 3 is missing.

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findErrorNums = function (nums) {
  let duplicate = -1;
  let missing = -1;
  const seen = new Set();

  for (const num of nums) {
    if (seen.has(num)) {
      duplicate = num;
    } else {
      seen.add(num);
    }
  }

  for (let value = 1; value <= nums.length; value++) {
    if (!seen.has(value)) {
      missing = value;
      break;
    }
  }

  return [duplicate, missing];
};

The first approach runs in O(n) time with the usual constant-time assumption for Set lookups and inserts. The set takes O(n) auxiliary space, and the input array is preserved.

Approach 2 · Mark seen values in place

The set records whether we have seen each value. Since every value v lies in 1…n, we can store that flag in the sign of nums[v - 1]. A positive sign means its corresponding value is unseen. A negative sign means it has already appeared.

  1. Read the current value with Math.abs(nums[i]). Its slot may have been marked earlier, and the absolute value recovers the original number.
  2. Look at the slot at index v - 1. If it is already negative, v is the duplicate. Otherwise, flip the slot to negative.
  3. Scan the marked array for its one positive slot. At index i, that slot represents the missing value i + 1.

For [1, 2, 2, 4], the first 2 flips nums[1] to -2, and the second 2 finds the sign already negative. The final array is [-1, -2, 2, -4]. The positive 2 sits at index 2, which represents the missing value 3.

Animated walkthrough · In-place sign marking

Each value marks its slot at value minus one. The second 2 points to an already negative slot. Index 2 stays positive, so 3 is missing.

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findErrorNums = function (nums) {
  let duplicate = -1;
  let missing = -1;

  for (let i = 0; i < nums.length; i++) {
    const value = Math.abs(nums[i]);
    const index = value - 1;

    if (nums[index] < 0) {
      duplicate = value;
    } else {
      nums[index] = -nums[index];
    }
  }

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] > 0) {
      missing = i + 1;
      break;
    }
  }

  return [duplicate, missing];
};

The second approach runs in O(n) time and uses O(1) auxiliary space. This version changes the signs in nums. If the caller needs the original values back, a final pass with Math.abs restores the array in O(n) time and O(1) auxiliary space.

Trade-off

ApproachTimeAuxiliary spaceInput
SetO(n)O(n)Preserved
In-place sign markingO(n)O(1)Signs changed

Takeaway

When optimizing memory, first look for a way to store the flag in data you already have. In Set Mismatch, v - 1 gives each value its own slot. The sign holds the seen flag, and the magnitude keeps the original value available through Math.abs. Reusing those slots removes the allocation for the set and cuts auxiliary space from O(n) to O(1). Both approaches still take O(n) time.

Related posts

Algorithms ·
Race Conditions in Production: Detection, Prevention, and Practical Solutions
Algorithms ·
Greedy Algorithm: Recognizing the Pattern Behind the Solution
Algorithms ·
String compression
← back to blog