Find Common Elements Between Two Arrays
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given two integer arrays nums1 and nums2 of sizes n and m, respectively. Calculate the following values:
answer1: the number of indicesisuch thatnums1[i]exists innums2.answer2: the number of indicesisuch thatnums2[i]exists innums1.
Return [answer1,answer2].
Examples
Example 1
Input: nums1 = [2,3,2], nums2 = [1,2]
Output: [2,1]
Explanation:

Example 2
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
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.lengthm == nums2.length1 <= n, m <= 1001 <= 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
- Convert
nums2to a set for quick lookup. - For each element in
nums1, check if it exists in the set and count such indices foranswer1. - Similarly, convert
nums1to a set and count indices innums2that exist innums1foranswer2. - Return
[answer1, answer2].
Code
C++
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};
}
};
Go
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}
}
Java
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};
}
}
Kotlin
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)
}
}
Python
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]
Rust
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]
}
}
TypeScript
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.