Skip to content

Commit 3384d99

Browse files
authored
feat: add csharp solution to lc problem: No.0350 (doocs#1925)
1 parent 18c4b00 commit 3384d99

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

solution/0300-0399/0350.Intersection of Two Arrays II/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,39 @@ class Solution {
224224
}
225225
```
226226

227+
### **C#**
228+
229+
```cs
230+
public class Solution {
231+
public int[] Intersect(int[] nums1, int[] nums2) {
232+
HashSet<int> hs1 = new HashSet<int>(nums1.Concat(nums2).ToArray());
233+
Dictionary<int, int> dict = new Dictionary<int, int>();
234+
List<int> result = new List<int>();
235+
236+
foreach (int x in hs1) {
237+
dict[x] = 0;
238+
}
239+
240+
foreach (int x in nums1) {
241+
if (dict.ContainsKey(x)) {
242+
dict[x] += 1;
243+
} else {
244+
dict[x] = 1;
245+
}
246+
}
247+
248+
foreach (int x in nums2) {
249+
if (dict[x] > 0) {
250+
result.Add(x);
251+
dict[x] -=1;
252+
}
253+
}
254+
255+
return result.ToArray();
256+
}
257+
}
258+
```
259+
227260
### **...**
228261

229262
```

solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md

+34
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,40 @@ class Solution {
213213
}
214214
```
215215

216+
### **C#**
217+
218+
```cs
219+
public class Solution {
220+
public int[] Intersect(int[] nums1, int[] nums2) {
221+
HashSet<int> hs1 = new HashSet<int>(nums1.Concat(nums2).ToArray());
222+
Dictionary<int, int> dict = new Dictionary<int, int>();
223+
List<int> result = new List<int>();
224+
225+
foreach (int x in hs1) {
226+
dict[x] = 0;
227+
}
228+
229+
foreach (int x in nums1) {
230+
if (dict.ContainsKey(x)) {
231+
dict[x] += 1;
232+
} else {
233+
dict[x] = 1;
234+
}
235+
}
236+
237+
foreach (int x in nums2) {
238+
if (dict[x] > 0) {
239+
result.Add(x);
240+
dict[x] -=1;
241+
}
242+
}
243+
244+
return result.ToArray();
245+
}
246+
}
247+
```
248+
249+
216250
### **...**
217251

218252
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
public int[] Intersect(int[] nums1, int[] nums2) {
3+
HashSet<int> hs1 = new HashSet<int>(nums1.Concat(nums2).ToArray());
4+
Dictionary<int, int> dict = new Dictionary<int, int>();
5+
List<int> result = new List<int>();
6+
7+
foreach (int x in hs1) {
8+
dict[x] = 0;
9+
}
10+
11+
foreach (int x in nums1) {
12+
if (dict.ContainsKey(x)) {
13+
dict[x] += 1;
14+
} else {
15+
dict[x] = 1;
16+
}
17+
}
18+
19+
foreach (int x in nums2) {
20+
if (dict[x] > 0) {
21+
result.Add(x);
22+
dict[x] -=1;
23+
}
24+
}
25+
26+
return result.ToArray();
27+
}
28+
}

0 commit comments

Comments
 (0)