Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sets/Set Mismatch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
The idea is using array indexing, that is putting each nums[i] into the position with index nums[i] - 1. Then, the array becomes [1,2,3,4,5...,n]. So we can find the duplicate number when nums[i] != i+1.

```
vector<int> findErrorNums(vector<int>& nums) {
for(int i = 0; i<nums.size(); i++){
while(nums[i] != nums[nums[i] - 1])swap(nums[i], nums[nums[i] - 1]);
}
for(int i = 0; i<nums.size() ; i++){
if(nums[i] != i + 1)return {nums[i], i + 1};
}
}
````