-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0350-IntersectionOfTwoArraysII.cs
39 lines (34 loc) · 1.09 KB
/
0350-IntersectionOfTwoArraysII.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 31.1 MB
// Link: https://leetcode.com/submissions/detail/344239069/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0350_IntersectionOfTwoArraysII
{
public int[] Intersect(int[] nums1, int[] nums2)
{
if (nums2.Length < nums1.Length) return Intersect(nums2, nums1);
var counts = new Dictionary<int, int>();
foreach (var num in nums1)
{
if (!counts.ContainsKey(num))
counts[num] = 1;
else
counts[num]++;
}
var result = new List<int>();
foreach (var num in nums2)
{
if (counts.ContainsKey(num) && counts[num] > 0)
{
result.Add(num);
counts[num]--;
}
}
return result.ToArray();
}
}
}