2956. Find Common Elements Between Two Arrays LeetCode Solution

In this guide, you will get 2956. Find Common Elements Between Two Arrays LeetCode Solution with the best time and space complexity. The solution to Find Common Elements Between Two Arrays problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Common Elements Between Two Arrays solution in C++
  4. Find Common Elements Between Two Arrays solution in Java
  5. Find Common Elements Between Two Arrays solution in Python
  6. Additional Resources
2956. Find Common Elements Between Two Arrays LeetCode Solution image

Problem Statement of Find Common Elements Between Two Arrays

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].

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.length
m == nums2.length
1 <= n, m <= 100
1 <= nums1[i], nums2[i] <= 100

Complexity Analysis

  • Time Complexity: O(n + m)
  • Space Complexity: O(n + m)

2956. Find Common Elements Between Two Arrays LeetCode Solution in C++

class Solution {
 public:
  vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
    const unordered_set<int> set1{nums1.begin(), nums1.end()};
    const unordered_set<int> set2{nums2.begin(), nums2.end()};
    const int ans1 = ranges::count_if(
        nums1, [&set2](int num) { return set2.contains(num); });
    const int ans2 = ranges::count_if(
        nums2, [&set1](int num) { return set1.contains(num); });
    return {ans1, ans2};
  }
};
/* code provided by PROGIEZ */

2956. Find Common Elements Between Two Arrays LeetCode Solution in Java

class Solution {
  public int[] findIntersectionValues(int[] nums1, int[] nums2) {
    Set<Integer> nums1Set = Arrays.stream(nums1).boxed().collect(Collectors.toSet());
    Set<Integer> nums2Set = Arrays.stream(nums2).boxed().collect(Collectors.toSet());
    final int ans1 = (int) Arrays.stream(nums1).filter(nums2Set::contains).count();
    final int ans2 = (int) Arrays.stream(nums2).filter(nums1Set::contains).count();
    return new int[] {ans1, ans2};
  }
}
// code provided by PROGIEZ

2956. Find Common Elements Between Two Arrays LeetCode Solution in Python

class Solution:
  def findIntersectionValues(
      self,
      nums1: list[int],
      nums2: list[int],
  ) -> list[int]:
    nums1Set = set(nums1)
    nums2Set = set(nums2)
    return [sum(num in nums2Set for num in nums1),
            sum(num in nums1Set for num in nums2)]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.