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: 1
Explanation: 
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: 4
Explanation: 
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}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Amortized analysis

Mathematics

Starter code: #User function Template for python3



#Function to return the count of the number of elements in

#the intersection of two arrays.

class Solution:

    def NumberofElementsInIntersection(self,a, b, n, m):

        #return: expected length of the intersection array.

        

        #code here

Solutions:
class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		y = list(set(a).intersection(set(b)))
		return len(y)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		a = set(a)
		b = set(b)
		ans = a.intersection(b)
		return len(ans)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		s1 = set(a)
		s2 = set(b)
		s3 = s1.intersection(s2)
		return len(s3)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		myset = set(a)
		myset1 = set(b)
		count = 0
		for i in myset1:
			if i in myset:
				count += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		s = set(a)
		d = set()
		for j in range(m):
			if b[j] in s:
				d.add(b[j])
		return len(d)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		c = []
		a = sorted(a)
		b = sorted(b)
		i = 0
		j = 0
		while i < n and j < m:
			if a[i] < b[j]:
				i += 1
			elif a[i] == b[j] and (len(c) == 0 or c[len(c) - 1] != a[i]):
				c.append(a[i])
				i += 1
				j += 1
			else:
				j += 1
		return len(c)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		c = set(a)
		d = set(b)
		e = c.intersection(d)
		return len(e)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		count = 0
		d = {}
		for i in a:
			if i not in d:
				d[i] = 1
		for i in b:
			if i in d:
				count += 1
				d.pop(i)
		return count


class Solution:

	def NumberofElementsInIntersection(self, array1, array2, n, m):
		this = set()
		result = list()
		for value in array1:
			this.add(value)
		for value in array2:
			if value in this:
				result.append(value)
				this.remove(value)
		return len(result)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		(helper_array, solution) = ([0] * 100000, 0)
		for i in a:
			helper_array[i + 1] = 1
		for i in b:
			if helper_array[i + 1] == 1:
				solution += 1
				helper_array[i + 1] = 0
		return solution


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		s = list(set(a) & set(b))
		return len(s)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		c = sorted(list(set(a).intersection(set(b))))
		return len(c)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		count = 0
		a = set(a)
		b = set(b)
		l = a.intersection(b)
		for i in l:
			count += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		res = set(a) & set(b)
		ans = list(res)
		return len(ans)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		count = 0
		hashmap = {}
		for num in a:
			hashmap[num] = 1
		for num in b:
			if num in hashmap and hashmap[num] != 2:
				count = count + 1
				hashmap[num] = 2
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		d = {}
		for i in a:
			if i not in d:
				d[i] = 1
		for i in b:
			if i in d:
				d[i] += 1
		res = 0
		for (x, y) in d.items():
			if y >= 2:
				res += 1
		return res


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		d = {}
		c = 0
		if n < m:
			for i in a:
				if i in d:
					d[i] = d[i] + 1
				else:
					d[i] = 1
			for i in b:
				if i in d:
					c = c + 1
					d.pop(i)
			return c
		else:
			for i in b:
				if i in d:
					d[i] = d[i] + 1
				else:
					d[i] = 1
			for i in a:
				if i in d:
					c = c + 1
					d.pop(i)
			return c


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		dict_1 = dict()
		dict_2 = dict()
		length = 0
		for number in a:
			dict_1[number] = dict_1.get(number, 0) + 1
		for number in b:
			dict_2[number] = dict_2.get(number, 0) + 1
		for key in dict_1:
			if dict_1.get(key, 0) > 0 and dict_2.get(key, 0) > 0:
				length += 1
		return length


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		set_a = set(a)
		set_b = set(b)
		set_intersect = set_a.intersection(set_b)
		return len(set_intersect)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		hs = set()
		c = 0
		for i in range(0, n):
			hs.add(a[i])
		for j in range(0, m):
			if b[j] in hs:
				c += 1
				hs.remove(b[j])
		return c


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		temp = set(a) & set(b)
		return len(temp)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		aa = set(a)
		bb = set(b)
		c = 0
		for i in aa:
			if i in bb:
				c += 1
		return c


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		if m < n:
			hm = {i: 1 for i in b}
			t = a
		else:
			hm = {i: 1 for i in a}
			t = b
		cnt = 0
		for i in t:
			if i in hm and hm[i] == 1:
				cnt += 1
				hm[i] = 0
		return cnt


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		d = {}
		l = []
		for i in b:
			d[i] = 1
		for i in a:
			if i in d:
				l.append(i)
				del d[i]
		return len(l)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		res = 0
		t = set(b)
		p = set(a)
		for i in p:
			if i in t:
				res += 1
		return res


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		A = set(a)
		B = set(b)
		c = A.intersection(B)
		return len(c)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		a1 = set(a)
		b1 = set(b)
		r = a1.intersection(b1)
		l = 0
		for i in r:
			l += 1
		return l


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		s1 = set(a)
		s2 = set(b)
		us = s1 & s2
		return len(us)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		set(a)
		set(b)
		common = 0
		common = set(a).intersection(set(b))
		return len(common)


from collections import *

class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		d = defaultdict(int)
		count = 0
		for i in set(a):
			d[i] += 1
		for i in set(b):
			d[i] += 1
		for i in set(a):
			if d[i] > 1:
				count += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		m = {}
		n = {}
		for i in a:
			m[i] = 0
		for i in b:
			n[i] = 0
		count = 0
		for i in m.keys():
			if i in n.keys():
				count += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		count = 0
		arr = list(set(a) & set(b))
		for i in range(len(arr)):
			count += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		dict1 = {}
		count = 0
		for i in range(n):
			dict1[a[i]] = 1
		for j in range(m):
			if b[j] in dict1:
				if dict1[b[j]] == 1:
					count += 1
					dict1[b[j]] += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		a.sort()
		b.sort()
		i = 0
		j = 0
		count = 0
		while i < n and j < m:
			if a[i] < b[j]:
				i += 1
			elif a[i] > b[j]:
				j += 1
			else:
				temp = a[i]
				while i < n and j < m and (a[i] == b[j]) and (a[i] == temp):
					i += 1
					j += 1
				count += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		x = set(a)
		y = set(b)
		z = x.intersection(y)
		return len(z)


class Solution:

	def bs(self, a, no):
		if len(a) == 0:
			return False
		ln = len(a)
		mid = ln // 2
		if a[mid] == no:
			return True
		elif a[mid] > no:
			return self.bs(a[:mid], no)
		else:
			return self.bs(a[mid + 1:], no)

	def NumberofElementsInIntersection(self, a, b, n, m):
		lis = set(a) & set(b)
		return len(lis)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		set1 = set(a)
		set2 = set(b)
		set3 = set1.intersection(set2)
		return len(set3)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		st1 = set(a)
		st2 = set(b)
		return len(st1 & st2)


from collections import Counter

class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		a_counter = Counter(a)
		b_counter = Counter(b)
		a_set = set(a)
		b_set = set(b)
		return len(a_set.intersection(b_set))


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		dic = dict()
		ans = 0
		if n < m:
			for x in a:
				if x not in dic:
					dic[x] = 0
			for x in b:
				if x in dic and dic[x] == 0:
					dic[x] += 1
					ans += 1
		else:
			for x in b:
				if x not in dic:
					dic[x] = 0
			for x in a:
				if x in dic and dic[x] == 0:
					dic[x] += 1
					ans += 1
		return ans


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		c = ()
		c = set(a)
		k = ()
		k = set(b)
		p = ()
		p = c.intersection(k)
		return len(p)


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		hash_set = set(a)
		count = 0
		for num in b:
			if num in hash_set:
				count += 1
				hash_set.remove(num)
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		if n > m:
			return len(set(a).intersection(b))
		else:
			return len(set(b).intersection(a))


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		ct = 0
		arrA = set(a)
		arrB = set(b)
		if n <= m:
			for i in arrA:
				if i in arrB:
					ct += 1
		else:
			for i in arrB:
				if i in arrA:
					ct += 1
		return ct


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		a.sort()
		b.sort()
		last = -float('inf')
		count = 0
		(i, j) = (0, 0)
		while i < n and j < m:
			if a[i] == b[j] and last != a[i]:
				last = a[i]
				count += 1
				i += 1
				j += 1
			elif a[i] < b[j]:
				i += 1
			else:
				j += 1
		return count


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		s1 = set()
		c = 0
		i = 0
		while i < n:
			s1.add(a[i])
			i += 1
		for i in range(m):
			if b[i] in s1:
				c += 1
				s1.remove(b[i])
		return c


class Solution:

	def NumberofElementsInIntersection(self, a, b, n, m):
		h_a = {}
		for el in a:
			h_a[el] = True
		res = 0
		for el in b:
			if el in h_a:
				res += h_a[el]
				h_a[el] = False
		return res