File tree 3 files changed +95
-0
lines changed
solution/0300-0399/0350.Intersection of Two Arrays II
3 files changed +95
-0
lines changed Original file line number Diff line number Diff line change @@ -224,6 +224,39 @@ class Solution {
224
224
}
225
225
```
226
226
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
+
227
260
### ** ...**
228
261
229
262
```
Original file line number Diff line number Diff line change @@ -213,6 +213,40 @@ class Solution {
213
213
}
214
214
```
215
215
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
+
216
250
### ** ...**
217
251
218
252
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments