sdfsfsf

# sdfsf

Reply

Reply

sdfsfsf

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.

public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { int count = 0; HashMap<Integer, Integer> twoSum1 = new HashMap<>(); HashMap<Integer, Integer> twoSum2 = new HashMap<>(); constructTwoPairMap(twoSum1, A, B); constructTwoPairMap(twoSum2, C, D); for (Integer pairSum : twoSum1.keySet()) { count += twoSum1.get(pairSum) * twoSum2.getOrDefault(pairSum * (-1), 0); } return count; } public void constructTwoPairMap(HashMap<Integer, Integer> map, int[] A, int[] B) { for (int i = 0; i < A.length; i++) { for (int j = 0; j < B.length; j++) { int sum = A[i] + B[j]; map.put(sum, map.getOrDefault(sum, 0) + 1); } } }