Algorithm


Problem Name: 645. Set Mismatch

You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

You are given an integer array nums representing the data status of this set after the error.

Find the number that occurs twice and the number that is missing and return them in the form of an array.

 

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

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        int n = nums.size(), sum = 0, dup = 0, miss = 0;
        vector<int>count(n, 0);
        for(int i = 0; i  <  n; sum += nums[i++]) if(++count[nums[i] - 1] > 1) dup = nums[i];
        miss = n * (n + 1) / 2 - sum + dup;
        return {dup, miss};
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,2,4]

Output

x
+
cmd
[2,3]

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int[] findErrorNums(int[] nums) {
    int[] result = new int[2];
    Set < Integer> set = new HashSet<>();
    int n = nums.length;
    for (int num : nums) {
      if (!set.add(num)) {
        result[0] = num;
      }
    }
    for (int i = 1; i  < = n; i++) {
      if (!set.contains(i)) {
        result[1] = i;
      }
    }
    return result;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,2,4]

Output

x
+
cmd
[2,3]

#3 Code Example with Javascript Programming

Code - Javascript Programming


const findErrorNums = function(nums) {
  if(nums == null || nums.length === 0) return null
  const res = []
  const hash = {}
  for(let el of nums) {
    if(hash.hasOwnProperty(el)){
      res[0] = el
    } else hash[el] = 0
    hash[el]++
  }
  for(let i = 1, len = nums.length; i <= len; i++) {
    if(!hash.hasOwnProperty(i)> {
      res[1] = i
      break
    }
  }
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,1]

Output

x
+
cmd
[1,2]

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findErrorNums(self, nums: List[int]) -> List[int]:
        cnt = collections.Counter(nums)
        return [k for k in cnt if cnt[k] == 2] + [i for i in range(1, len(nums) + 1) if i not in cnt]
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,1]

Output

x
+
cmd
[1,2]

#5 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0645_SetMismatch
    {
        public int[] FindErrorNums(int[] nums)
        {
            int duplicate = -1, missing = -1;
            foreach (int num in nums)
            {
                if (nums[Math.Abs(num) - 1]  <  0)
                    duplicate = Math.Abs(num);
                else
                    nums[Math.Abs(num) - 1] *= -1;
            }

            for (int i = 0; i  <  nums.Length; i++)
                if (nums[i] > 0)
                    missing = i + 1;

            return new int[] { duplicate, missing };
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,2,4]

Output

x
+
cmd
[2,3]
Advertisements

Demonstration


Previous
#643 Leetcode Maximum Average Subarray I Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#646 Leetcode Maximum Length of Pair Chain Solution in C, C++, Java, JavaScript, Python, C# Leetcode