Set Mismatch

salahelhossiny

SalahElhossiny

Posted on August 27, 2022

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.


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
                break 

        return [res, findMissing(nums)]






Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
salahelhossiny
SalahElhossiny

Posted on August 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related