# Set Mismatch using Python

Set Mismatch is a popular question in coding interviews. In this problem, you need to find two numbers in an array that are missing or duplicated. So, if you want to know how to solve this problem using Python, this article is for you. In this article, I will take you through how to Set Mismatch using Python.

## Set Mismatch Problem

In the Set Mismatch problem, you will be given an array of positive integers with some elements missing or duplicated. You need to identify and return the missing and duplicated numbers to solve this problem.

Here’s an example of the input and output values of this problem:

• Input: [1, 2, 3, 4, 4, 6] | Output: [5, 4]

In this example, the missing number is 5 since it is the only positive integer from 1 to 6 that is not present in the array, and the duplicate number is 4 since it appears twice.

## Set Mismatch using Python

I hope you have understood what the Set Mismatch problem means. Now here’s how to solve this problem using Python:

```def findErrorNums(nums):
n = len(nums)

duplicate = -1
for num in nums:
if nums[abs(num) - 1] < 0:
duplicate = abs(num)
else:
nums[abs(num) - 1] *= -1

missing = -1
for i in range(n):
if nums[i] > 0:
missing = i + 1
return [duplicate, missing]

nums = [1,2,2,4]
print(findErrorNums(nums))```
`Output: [2, 3]`

The above function iterates through the array twice. In the first iteration, it marks the duplicated number as negative by multiplying its value by -1. In the second iteration, it finds the missing number by checking for a positive number with an index that is one more than the missing number. In the end, it returns the duplicated number and the missing number as an array.

So this is how you can solve the Set Mismatch problem using Python. You can find many more practice questions for coding interviews solved and explained using Python here.

### Summary

In the Set Mismatch problem, you will be given an array of positive integers with some elements missing or duplicated. You need to identify and return the missing and duplicated numbers to solve this problem. I hope you liked this article on how to solve the Set Mismatch problem using Python. Feel free to ask valuable questions in the comments section below.

##### Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of dataðŸ“ˆ.

Articles: 1435