Intersection of Two Arrays
Question
Analysis
Solution
// HashMap Approach
public class Solution {
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
HashMap<Integer, Boolean> map1 = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> intersectMap = new HashMap<Integer, Boolean>();
for (int i = 0; i < nums1.length; i++) {
if (!map1.containsKey(nums1[i])) {
map1.put(nums1[i], true);
}
}
for (int j = 0; j < nums2.length; j++) {
if (map1.containsKey(nums2[j]) && !intersectMap.containsKey(nums2[j])) {
intersectMap.put(nums2[j], true);
}
}
int[] result = new int[intersectMap.size()];
int i = 0;
for (Integer e : intersectMap.keySet()) {
result[i] = e;
i++;
}
return result;
}
}Last updated