---
# System prepended metadata

title: 0645. Set Mismatch
tags: [Leetcode, Indexing Sort, Easy]

---

# 0645. Set Mismatch
###### tags: `Leetcode` `Easy` `Indexing Sort`
Link: https://leetcode.com/problems/set-mismatch/description/
## 思路
和[0442. Find All Duplicates in an Array](https://hackmd.io/UnpI8ph4Q6icK2K1UxDHfg)差不多
## Code
```java=
class Solution {
    public int[] findErrorNums(int[] nums) {
        int[] ans = new int[2];
        int n = nums.length;
        for(int i=0; i<n; i++){
            if(nums[i]!=i+1){
                while(nums[i]!=nums[nums[i]-1]){
                    swap(nums, i, nums[i]-1);
                }
            }
        }
        for(int i=0; i<n; i++){
            if(nums[i]!=i+1){
                ans[0] = nums[i];
                ans[1] = i+1;
            }
        }
        return ans;
    }
    private void swap(int[] nums, int idx1, int idx2){
        int temp = nums[idx1];
        nums[idx1] = nums[idx2];
        nums[idx2] = temp;
    }
}
```