/* * 349. Intersection of Two Arrays * 2016-7-12 by Mingyang * I II都非常的简单,因为就是很简单,第一个题目用HashSet来做,去掉重复的加入unique的就好了 * 第二个就只需要加入list,用两个指针来做 */ public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set = new HashSet<>(); Set<Integer> intersect = new HashSet<>(); for (int i = 0; i < nums1.length; i++) { set.add(nums1[i]); } for (int i = 0; i < nums2.length; i++) { if (set.contains(nums2[i])) { intersect.add(nums2[i]); } } int[] result = new int[intersect.size()]; int i = 0; for (Integer num : intersect) { result[i++] = num; } return result; }