Skip to content

Commit

Permalink
Add C# solution of 350
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahir509 committed Nov 4, 2023
1 parent a27055d commit 997c6ad
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
39 changes: 39 additions & 0 deletions solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,45 @@ class Solution {
}
```

### **C#**
```c#
public class Solution {
public int[] Intersect(int[] nums1, int[] nums2) {
HashSet<int> hs1 = new HashSet<int>(nums1.Concat(nums2).ToArray());
Dictionary<int,int> dict = new Dictionary<int,int>();
List<int> result = new List<int>();

foreach(int value in hs1){
dict[value] = 0;
}

foreach(int value in nums1)
{
if(dict.ContainsKey(value))
{
dict[value]+=1;
}
else
{
dict[value] = 1;
}
}

foreach(int value in nums2)
{
if(dict[value] > 0)
{
result.Add(value);
dict[value] -=1;
}
}

return result.ToArray();
}
}
```


### **...**

```
Expand Down
34 changes: 34 additions & 0 deletions solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Solution {
public int[] Intersect(int[] nums1, int[] nums2) {
HashSet<int> hs1 = new HashSet<int>(nums1.Concat(nums2).ToArray());
Dictionary<int,int> dict = new Dictionary<int,int>();
List<int> result = new List<int>();

foreach(int value in hs1){
dict[value] = 0;
}

foreach(int value in nums1)
{
if(dict.ContainsKey(value))
{
dict[value]+=1;
}
else
{
dict[value] = 1;
}
}

foreach(int value in nums2)
{
if(dict[value] > 0)
{
result.Add(value);
dict[value] -=1;
}
}

return result.ToArray();
}
}

0 comments on commit 997c6ad

Please sign in to comment.