2019年1月28日
return nums1.Intersect(nums2).ToArray();
今天重新整理了博客 :
2018年12月17日
1 、第一个数组存到 HashSet 中
2、将第二个数组等于set的就加到一个集合里
3、将集合转为数组返回
static void Main(string[] args)
{
int[] oos = new int[] { 9, 4, 9, 8, 4 };
int[] oop = new int[] { 9, 4 };
int[]s= Intersection(oos, oop);
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i]);
}
Console.ReadKey();
}
public static int[] Intersection(int[] nums1, int[] nums2)
{
HashSet<int> set = new HashSet<int>();
for (int p = 0; p < nums1.Length; p++)
set.Add(nums1[p]);
List<int> resultList = new List<int>();
for (int z = 0; z < nums2.Length; z++)
{
if (set.Contains(nums2[z]))
{
resultList.Add(nums2[z]);
set.Remove(nums2[z]);
}
}
int[] num = new int[resultList.Count] ;
if (num.Count() <= 0)
return num;
for (int i = 0; i < resultList.Count; i++)
{
num[i] = resultList[i];
}
return num;
}
请参考我2019年1/30新写的方法https://blog.youkuaiyun.com/us2019/article/details/80538723