LeetCode – Intersection of Two Arrays II (Java)
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Java Solution 1 public int[] intersect(int[] nums1, int[] nums2) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i: nums1){ if(map.containsKey(i)){ map.put(i, map.get(i)+1); }else{ map.put(i, 1); } } ArrayList<Integer> … Read more