> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Set Mismatch Python Solution with Tests

> Tested Python solution for LeetCode 645 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 645, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Bit Manipulation](/catalog/topics/bit-manipulation), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/set-mismatch/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 645   # by problem number
lcpy gen -s set_mismatch   # by problem name
```

## Problem

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*.

### Examples

```
Input: nums = [1,2,2,4]
Output: [2,3]
```

```
Input: nums = [1,1]
Output: [1,2]
```

### Constraints

* 2 \<= nums.length \<= 10^4
* 1 \<= nums\[i] \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/set_mismatch/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/set_mismatch/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(n)
    def find_error_nums(self, nums: list[int]) -> list[int]:
        n = len(nums)
        counts = Counter(nums)
        duplicate = missing = -1
        for value in range(1, n + 1):
            if counts[value] == 2:
                duplicate = value
            elif counts[value] == 0:
                missing = value
        return [duplicate, missing]
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.