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.

**Input:**

The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and M, N is the size of array A and M is the size of array B. The second line of each test case contains N input A[i].

The third line of each test case contains M input B[i].

**Output:**

Print the count of intersecting elements.

**User Task:**

The task is to complete the function **NumberofElementsInIntersection** which takes 4 inputs ie- array a, array b, n which is the size of array a, m which is the size of array b. 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 ≤ T ≤ 100

1 ≤ N, M ≤ 10^{5}

1 ≤ A[i], B[i] ≤ 10^{5}

**Example:**

**Input:**

4

5 3

89 24 75 11 23

89 2 4

6 5

1 2 3 4 5 6

3 4 5 6 7

4 4

10 10 10 10

20 20 20 20

3 3

10 10 10

10 10 10

**Output:**

1

4

0

1

**Explanation:
Testcase 1:** 89 is the only element in the intersection of two arrays.

Author: rajatjha

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

sakshamarora333 | 36 |

soul_mortal | 23 |

MadhusudhanGupta | 20 |

poojasakhare278 | 16 |

kid_kb | 16 |

descifrador | 1683 |

mantu_singh | 1476 |

manvirk525 | 1205 |

rishabh2017 | 1119 |

gladiator121 | 1104 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4974 |

Quandray | 4720 |

Login to report an issue on this page.