Skip to content

Commit 60a6c30

Browse files
authored
Merge pull request #3941 from ImmidiSivani/leetcode-645
solution added to 645
2 parents eb94b57 + f168a17 commit 60a6c30

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
id: set mismatch
3+
title: Set Mismatch
4+
sidebar_label: 645-Set Mismatch
5+
tags:
6+
- Arrays
7+
- Hashing
8+
- LeetCode
9+
- Java
10+
- Python
11+
- C++
12+
description: "This is a solution to the Set Mismatch problem on LeetCode."
13+
sidebar_position: 3
14+
---
15+
16+
## Problem Description
17+
18+
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.
19+
20+
You are given an integer array `nums` representing the data status of this set after the error.
21+
22+
Find the number that occurs twice and the number that is missing and return them in the form of an array.
23+
24+
### Examples
25+
26+
**Example 1:**
27+
28+
```
29+
Input: nums = [1,2,2,4]
30+
Output: [2,3]
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input: nums = [1,1]
37+
Output: [1,2]
38+
```
39+
40+
### Constraints
41+
42+
- `2 <= nums.length <= 10^4`
43+
- `1 <= nums[i] <= 10^4`
44+
45+
---
46+
47+
## Solution for Find Error Numbers Problem
48+
49+
To solve this problem, you need to identify the duplicated number and the missing number in the array.
50+
51+
### Approach: Counting Frequency
52+
53+
1. **Count Frequencies:** Use a dictionary or a list to count the frequency of each number in the array.
54+
2. **Identify Duplicated and Missing Numbers:**
55+
- The number with a frequency of 2 is the duplicated number.
56+
- The number with a frequency of 0 (among the numbers from 1 to n) is the missing number.
57+
58+
### Brute Force Approach
59+
60+
The brute force approach involves iterating over the numbers from 1 to `n` and checking their frequency in the given array. This can be achieved by:
61+
62+
1. Initializing an array to count the frequency of each number.
63+
2. Iterating through the input array to update the frequency count.
64+
3. Checking which number has a frequency of 2 (the duplicated number) and which number has a frequency of 0 (the missing number).
65+
66+
### Optimized Approach
67+
68+
The optimized approach avoids using extra space and iterates through the input array only twice:
69+
70+
1. Iterate through the array and mark the corresponding indices as visited by flipping the sign of the elements.
71+
2. In the second pass, the index with a positive value indicates the missing number, and the duplicate can be identified by the repeated index encountered in the first pass.
72+
73+
### Code in Different Languages
74+
75+
<Tabs>
76+
<TabItem value="C++" label="C++" default>
77+
<SolutionAuthor name="@ImmidiSivani"/>
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
vector<int> findErrorNums(vector<int>& nums) {
83+
int n = nums.size();
84+
vector<int> freq(n + 1, 0);
85+
vector<int> result(2, 0);
86+
87+
88+
for (int num : nums) {
89+
freq[num]++;
90+
}
91+
92+
93+
for (int i = 1; i <= n; i++) {
94+
if (freq[i] == 2) result[0] = i;
95+
else if (freq[i] == 0) result[1] = i;
96+
}
97+
98+
return result;
99+
}
100+
};
101+
```
102+
103+
</TabItem>
104+
<TabItem value="Java" label="Java">
105+
<SolutionAuthor name="@ImmidiSivani"/>
106+
107+
```java
108+
class Solution {
109+
public int[] findErrorNums(int[] nums) {
110+
int[] freq = new int[nums.length + 1];
111+
int[] result = new int[2];
112+
113+
114+
for (int num : nums) {
115+
freq[num]++;
116+
}
117+
118+
119+
for (int i = 1; i < freq.length; i++) {
120+
if (freq[i] == 2) result[0] = i;
121+
else if (freq[i] == 0) result[1] = i;
122+
}
123+
124+
return result;
125+
}
126+
}
127+
```
128+
129+
</TabItem>
130+
<TabItem value="Python" label="Python">
131+
<SolutionAuthor name="@ImmidiSivani"/>
132+
133+
```python
134+
class Solution:
135+
def findErrorNums(self, nums: List[int]) -> List[int]:
136+
freq = [0] * (len(nums) + 1)
137+
result = [0, 0]
138+
139+
140+
for num in nums:
141+
freq[num] += 1
142+
143+
144+
for i in range(1, len(freq)):
145+
if freq[i] == 2:
146+
result[0] = i
147+
elif freq[i] == 0:
148+
result[1] = i
149+
150+
return result
151+
```
152+
153+
</TabItem>
154+
</Tabs>
155+
156+
#### Complexity Analysis
157+
158+
- **Time Complexity**: $O(n)$, where `n` is the length of the input array.
159+
- **Space Complexity**: $O(n)$, due to the frequency array used to count occurrences of each number.
160+
161+
---
162+
163+
<h2>Authors:</h2>
164+
165+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
166+
{['ImmidiSivani'].map(username => (
167+
<Author key={username} username={username} />
168+
))}
169+
</div>

0 commit comments

Comments
 (0)