LeetCode-C#实现-哈希表(#349)
2021-06-26 22:04
标签:color reac solution 相等 title etc for set add 两个数组的交集 LeetCode-C#实现-哈希表(#349) 标签:color reac solution 相等 title etc for set add 原文地址:https://www.cnblogs.com/errornull/p/10095741.html349. Intersection of Two Arrays
public class Solution {
public int[] Intersection(int[] nums1, int[] nums2) {
//使用哈希集合去重
HashSetint> hashSet1=new HashSetint>(nums1);
HashSetint> hashSet2=new HashSetint>(nums2);
Listint> list=new Listint>();
//遍历两哈希集合,相等者加入集合,遍历结束后将集合转为数组返回
foreach(int h1 in hashSet1){
foreach(int h2 in hashSet2){
if(h1==h2)list.Add(h1);
}
}
return list.ToArrayint>();
}
}