LeetCode-C#实现-哈希表(#349)
349. Intersection of Two Arrays
两个数组的交集
public class Solution { public int[] Intersection(int[] nums1, int[] nums2) { //使用哈希集合去重 HashSet<int> hashSet1=new HashSet<int>(nums1); HashSet<int> hashSet2=new HashSet<int>(nums2); List<int> list=new List<int>(); //遍历两哈希集合,相等者加入集合,遍历结束后将集合转为数组返回 foreach(int h1 in hashSet1){ foreach(int h2 in hashSet2){ if(h1==h2)list.Add(h1); } } return list.ToArray<int>(); } }