Problem

You are given two integer arrays nums1 and nums2 of sizes n and m, respectively. Calculate the following values:

  • answer1 : the number of indices i such that nums1[i] exists in nums2.
  • answer2 : the number of indices i such that nums2[i] exists in nums1.

Return [answer1,answer2].

Examples

Example 1

1
2
3
4
5
6
7
8

Input: nums1 = [2,3,2], nums2 = [1,2]

Output: [2,1]

Explanation:

![](https://assets.leetcode.com/uploads/2024/05/26/3488_find_common_elements_between_two_arrays-t1.gif)

Example 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]

Output: [3,4]

Explanation:

The elements at indices 1, 2, and 3 in `nums1` exist in `nums2` as well. So
`answer1` is 3.

The elements at indices 0, 1, 3, and 4 in `nums2` exist in `nums1`. So
`answer2` is 4.

Example 3

1
2
3
4
5
6
7
8

Input: nums1 = [3,4,2,3], nums2 = [1,5]

Output: [0,0]

Explanation:

No numbers are common between `nums1` and `nums2`, so answer is [0,0].

Constraints

  • n == nums1.length
  • m == nums2.length
  • 1 <= n, m <= 100
  • 1 <= nums1[i], nums2[i] <= 100

Solution

Method 1 – Hash Set Lookup 1

Intuition

To efficiently check if an element from one array exists in the other, we can use hash sets for constant-time lookups.

Approach

  1. Convert nums2 to a set for quick lookup.
  2. For each element in nums1, check if it exists in the set and count such indices for answer1.
  3. Similarly, convert nums1 to a set and count indices in nums2 that exist in nums1 for answer2.
  4. Return [answer1, answer2].

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
public:
    vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> s2(nums2.begin(), nums2.end()), s1(nums1.begin(), nums1.end());
        int ans1 = 0, ans2 = 0;
        for (int n : nums1) if (s2.count(n)) ans1++;
        for (int n : nums2) if (s1.count(n)) ans2++;
        return {ans1, ans2};
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func findIntersectionValues(nums1, nums2 []int) []int {
    s2 := make(map[int]struct{})
    for _, n := range nums2 {
        s2[n] = struct{}{}
    }
    ans1 := 0
    for _, n := range nums1 {
        if _, ok := s2[n]; ok {
            ans1++
        }
    }
    s1 := make(map[int]struct{})
    for _, n := range nums1 {
        s1[n] = struct{}{}
    }
    ans2 := 0
    for _, n := range nums2 {
        if _, ok := s1[n]; ok {
            ans2++
        }
    }
    return []int{ans1, ans2}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
    public int[] findIntersectionValues(int[] nums1, int[] nums2) {
        Set<Integer> s2 = new HashSet<>();
        for (int n : nums2) s2.add(n);
        int ans1 = 0;
        for (int n : nums1) if (s2.contains(n)) ans1++;
        Set<Integer> s1 = new HashSet<>();
        for (int n : nums1) s1.add(n);
        int ans2 = 0;
        for (int n : nums2) if (s1.contains(n)) ans2++;
        return new int[]{ans1, ans2};
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    fun findIntersectionValues(nums1: IntArray, nums2: IntArray): IntArray {
        val s2 = nums2.toSet()
        val ans1 = nums1.count { it in s2 }
        val s1 = nums1.toSet()
        val ans2 = nums2.count { it in s1 }
        return intArrayOf(ans1, ans2)
    }
}
1
2
3
4
5
6
7
class Solution:
    def findIntersectionValues(self, nums1: list[int], nums2: list[int]) -> list[int]:
        s2 = set(nums2)
        ans1 = sum(n in s2 for n in nums1)
        s1 = set(nums1)
        ans2 = sum(n in s1 for n in nums2)
        return [ans1, ans2]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
impl Solution {
    pub fn find_intersection_values(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
        use std::collections::HashSet;
        let s2: HashSet<_> = nums2.iter().collect();
        let ans1 = nums1.iter().filter(|n| s2.contains(n)).count() as i32;
        let s1: HashSet<_> = nums1.iter().collect();
        let ans2 = nums2.iter().filter(|n| s1.contains(n)).count() as i32;
        vec![ans1, ans2]
    }
}
1
2
3
4
5
6
7
8
9
class Solution {
    findIntersectionValues(nums1: number[], nums2: number[]): number[] {
        const s2 = new Set(nums2);
        const ans1 = nums1.filter(n => s2.has(n)).length;
        const s1 = new Set(nums1);
        const ans2 = nums2.filter(n => s1.has(n)).length;
        return [ans1, ans2];
    }
}

Complexity

  • ⏰ Time complexity: O(n + m), where n and m are the lengths of the arrays, for set construction and lookups.
  • 🧺 Space complexity: O(n + m), for storing the sets.