File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
solution/0300-0399/0350.Intersection of Two Arrays II Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -213,6 +213,45 @@ class Solution {
213
213
}
214
214
```
215
215
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
+
216
255
### ** ...**
217
256
218
257
```
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 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
+ }
You can’t perform that action at this time.
0 commit comments