# C#LeetCode刷题之#349-两个数组的交集（Intersection of Two Arrays）

Given two arrays, write a function to compute their intersection.

Input: nums1 = [1,2,2,1], nums2 = [2,2]

Output: [2]

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Output: [9,4]

Note:

Each element in the result must be unique.
The result can be in any order.

```public class Program {

public static void Main(string[] args) {
var nums1 = new int[] { 1, 2, 2, 1 };
var nums2 = new int[] { 2, 2 };

var res = Intersection(nums1, nums2);
ShowArray(res);

nums1 = new int[] { 4, 9, 5 };
nums2 = new int[] { 9, 4, 9, 8, 4 };

res = Intersection2(nums1, nums2);
ShowArray(res);

}

private static void ShowArray(int[] array) {
foreach(var num in array) {
Console.Write(\$"{num} ");
}
Console.WriteLine();
}

private static int[] Intersection(int[] nums1, int[] nums2) {
var set = new HashSet<int>();
foreach(var i in nums1) {
}
var list = new List<int>();
var set2 = new HashSet<int>();
foreach(var k in nums2) {
if(set.Contains(k) && !set2.Contains(k)) {
}
}
return list.ToArray();
}

private static int[] Intersection2(int[] nums1, int[] nums2) {
return nums1.Intersect(nums2).ToArray();
}

}```

```2
4 9```

(2)