Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given two arrays **a[]** and **b[]** respectively of size **n** and **m, **the task is to print the count of elements in the intersection (or common elements) of the two arrays.

For this question, the intersection of two arrays can be defined as the set containing distinct common elements between the two arrays.

**Example 1:**

Input:n = 5, m = 3 a[] = {89, 24, 75, 11, 23} b[] = {89, 2, 4}Output:1Explanation:89 is the only element in the intersection of two arrays.

**Example 2:**

Input:n = 6, m = 5 a[] = {1, 2, 3, 4, 5, 6} b[] = {3, 4, 5, 6, 7}Output:4Explanation:3 4 5 and 6 are the elements in the intersection of two arrays.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **NumberofElementsInIntersection()**** **which takes two integers **n** and **m** and their respective arrays **a[] **and **b[]** as input. The function should return the count of the number of elements in the intersection.

**Expected Time Complexity: **O(n + m).

**Expected Auxiliary Space: **O(min(n,m)).

**Constraints:**

1 ≤ n, m ≤ 10^{5}

1 ≤ a[i], b[i] ≤ 10^{5}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Intersection of two arrays

...