首页 > 代码库 > 349.求两个数组的交集 Intersection of Two Arrays
349.求两个数组的交集 Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
Subscribe to see which companies asked this question
public int[] Intersection(int[] nums1, int[] nums2) {
List<int> l = new List<int>();
List<int> sameList = new List<int>();
int length = nums1.Length;
for (int i = 0; i < length; i++) {
l.Add(nums1[i]);
}
length = nums2.Length;
int num = 0;
for (int i = 0; i < length; i++) {
num = nums2[i];
if (l.Contains(num) && !sameList.Contains(num)) {
sameList.Add(num);
}
}
int [] list = sameList.ToArray();
return list;
}
来自为知笔记(Wiz)
349.求两个数组的交集 Intersection of Two Arrays
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。