首页 > 代码库 > 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

  1. public int[] Intersection(int[] nums1, int[] nums2) {
  2. List<int> l = new List<int>();
  3. List<int> sameList = new List<int>();
  4. int length = nums1.Length;
  5. for (int i = 0; i < length; i++) {
  6. l.Add(nums1[i]);
  7. }
  8. length = nums2.Length;
  9. int num = 0;
  10. for (int i = 0; i < length; i++) {
  11. num = nums2[i];
  12. if (l.Contains(num) && !sameList.Contains(num)) {
  13. sameList.Add(num);
  14. }
  15. }
  16. int [] list = sameList.ToArray();
  17. return list;
  18. }



来自为知笔记(Wiz)


349.求两个数组的交集 Intersection of Two Arrays