Set Mismatch

August 27, 2022

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.

from collections import Counter

class Solution(object):
    def findErrorNums(self, nums):
        :type nums: List[int]
        :rtype: List[int]

        def findMissing(ns):
            for i in range(1, len(ns) + 1):
                if i not in ns:
                    return i

        c = Counter(nums).items()
        res = 0 

        for k, v in c:
            if v == 2:
                res = k

        return [res, findMissing(nums)]

Source link

Comments 0

Leave a Reply

Your email address will not be published. Required fields are marked *