Skip to content

Latest commit

 

History

History
 
 

645. Set Mismatch

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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

Related Topics:
Hash Table, Math

Similar Questions:

Solution 1. unordered_map

// OJ: https://leetcode.com/problems/set-mismatch
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
    vector<int> findErrorNums(vector<int>& A) {
        unordered_map<int, int> m;
        for (auto n : A) m[n]++;
        int missing, dup;
        for (int i = 1; i <= A.size(); ++i) {
            if (!m.count(i)) missing = i;
            if (m[i] == 2) dup = i;
        }
        return { dup, missing };
    }
};

Solution 2. Sort

// OJ: https://leetcode.com/problems/set-mismatch/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
    vector<int> findErrorNums(vector<int>& A) {
        sort(begin(A), end(A));
        int dup, missing = 1;
        for (int i = 1; i < A.size(); ++i) {
            if (A[i] == A[i - 1]) dup = A[i];
            else if (A[i] > A[i - 1] + 1) missing = A[i - 1] + 1;
        }
        return { dup, A.back() != A.size() ? (int)A.size() : missing };
    }
};

Solution 3. Flip Sign

If we can encode the Counts Array into nums, we can save space.

When we visit a number n in nums, we invert nums[abs(n) - 1]. If the number n just appears once, the corresponding nums[abs(n) - 1] will be negative. When we see a negative nums[abs(n) - 1], it means n has been visited -- it's the duplicate.

If nums[i] is left positive, then i + 1 is the missing number.

// OJ: https://leetcode.com/problems/set-mismatch/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        int dup, missing;
        for (int n : nums) {
            if (nums[abs(n) - 1] < 0) dup = abs(n);
            else nums[abs(n) - 1] *= -1;
        }
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] > 0) missing = i + 1;
        }
        return { dup, missing };
    }
};

Solution 3. Swap

// OJ: https://leetcode.com/problems/set-mismatch/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    vector<int> findErrorNums(vector<int>& A) {
        int N = A.size();
        for (int i = 0; i < A.size(); ++i) {
            while (A[i] != i + 1 && A[A[i] - 1] != A[i]) swap(A[i], A[A[i] - 1]);
        }
        for (int i = 0; i < A.size(); ++i) {
            if (A[i] != i + 1) return {A[i], i + 1};
        }
        return {};
    }
};