Skip to content

Commit 997c6ad

Browse files
committed
Add C# solution of 350
1 parent a27055d commit 997c6ad

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,45 @@ class Solution {
213213
}
214214
```
215215

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

218257
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 value in hs1){
8+
dict[value] = 0;
9+
}
10+
11+
foreach(int value in nums1)
12+
{
13+
if(dict.ContainsKey(value))
14+
{
15+
dict[value]+=1;
16+
}
17+
else
18+
{
19+
dict[value] = 1;
20+
}
21+
}
22+
23+
foreach(int value in nums2)
24+
{
25+
if(dict[value] > 0)
26+
{
27+
result.Add(value);
28+
dict[value] -=1;
29+
}
30+
}
31+
32+
return result.ToArray();
33+
}
34+
}

0 commit comments

Comments
 (0)