Given a N x N matrix M. Write a program to find count of all the distinct elements common to all rows of the matrix. Print count of such elements.
Example 1:
Input: 
N = 4
M = {{2, 1, 4, 3},
     {1, 2, 3, 2},
     {3, 6, 2, 3},
     {5, 2, 5, 3}}
Output: 
2
Explaination: Only 2 and 3 are common in all rows.
Example 2:
Input: 
N = 5
M = {{12, 1, 14, 3, 16},
     {14, 2, 1, 3, 35},
     {14, 1, 14, 3, 11},
     {14, 5, 3, 2, 1},
     {1, 18, 3, 21, 14}}
Output: 
3
Explaination: 14, 3 and 1 are common in all the rows.
Your Task:
You do not need to read input or print anything. Your task is to complete the function distinct() which takes the matrix and n as input parameters and returns the number of distinct elements present in every row.
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 100
1 ≤ M[i][j] ≤ 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def distinct(self, M, N):

        # code here

Solutions:
class Solution:

	def distinct(self, M, N):
		d = dict()
		for i in range(N):
			if M[0][i] not in d:
				d[M[0][i]] = 0
		for i in range(N):
			dp = dict()
			for j in range(N):
				if M[i][j] not in dp:
					dp[M[i][j]] = 1
			for key in dp.keys():
				if key in d:
					d[key] += 1
		c = 0
		for (keys, values) in d.items():
			if values >= N:
				c += 1
		return c


class Solution:

	def distinct(self, M, N):
		init = set(M[0])
		result = set(M[0])
		for idx in range(1, N):
			x = M[idx]
			if len(result) == 0:
				return 0
			un = set(x)
			for y in init:
				if y not in un:
					if y in result:
						result.remove(y)
		return len(result)


class Solution:

	def distinct(self, M, N):
		d = dict()
		for x in M:
			un = set(x)
			for y in un:
				if y in d:
					d[y] += 1
				else:
					d[y] = 1
		count = 0
		for i in d:
			if d[i] == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		d = {}
		for i in range(N):
			d[M[0][i]] = 0
		for i in range(1, N):
			for j in range(N):
				if M[i][j] in d:
					d[M[i][j]] += 1
			r = []
			for (i, j) in d.items():
				if j == 0:
					r.append(i)
				else:
					d[i] = 0
			for i in r:
				del d[i]
		r = len(d)
		return r


class Solution:

	def distinct(self, M, N):
		table = {}
		for i in M[0]:
			table[i] = 1
		for i in range(N):
			for j in range(N):
				p = M[i][j]
				if p in table and table[p] == i:
					table[p] += 1
		count = 0
		for i in table:
			if table[i] == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		times = set()
		for j in range(N):
			times.add(M[0][j])
		for i in range(1, N):
			temp = set()
			for j in range(N):
				temp.add(M[i][j])
			times = times.intersection(temp)
		return len(times)


class Solution:

	def distinct(self, M, N):
		union = set(M[0])
		for index in range(1, N):
			union = union & set(M[index])
		return len(union)


import numpy as np

class Solution:

	def distinct(self, M, N):
		le1 = len(M)
		le2 = len(M[0])
		ans = M[0]
		for i in range(1, le1):
			ans = np.intersect1d(M[i], ans)
		return len(ans)


class Solution:

	def distinct(self, M, N):
		s = set(M[0])
		for i in range(1, N):
			s = s & set(M[i])
		return len(s)


class Solution:

	def distinct(self, M, N):
		dic = set(M[0])
		for i in range(1, N):
			temp = set()
			for j in M[i]:
				if j in dic:
					temp.add(j)
			dic = temp
		return len(dic)


class Solution:

	def distinct(self, mat, n):
		s = set(mat[0])
		for i in range(n):
			s = s & set(mat[i])
		return len(s)


class Solution:

	def distinct(self, mat, n):
		freq = {}
		for num in mat[0]:
			freq[num] = 1
		for i in range(1, n):
			for num in set(mat[i]):
				if num in freq:
					freq[num] += 1
		count = 0
		for (num, f) in freq.items():
			if f == n:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		ans = []
		temp = M[0]
		for i in temp:
			flag = 0
			for j in range(1, N):
				if i not in M[j]:
					flag = 1
					break
			if flag == 0 and i not in ans:
				ans.append(i)
		return len(ans)


class Solution:

	def distinct(self, M, N):
		c = 0
		d = set(M[0])
		for i in d:
			s = 0
			for j in M:
				if i in j:
					s += 1
			if s == N:
				c += 1
		return c


class Solution:

	def distinct(self, m, n):
		f = 0
		for i in m:
			if f:
				s = s.intersection(set(i))
			else:
				s = set(i)
				f = 1
		return len(s)


class Solution:

	def distinct(self, M, N):
		cnt = 0
		s = []
		for i in M:
			s.append(set(i))
		for i in s[0]:
			flg = 0
			for j in s[1:]:
				if i not in j:
					flg = 1
					break
			if flg == 0:
				cnt += 1
		return cnt


class Solution:

	def distinct(self, M, N):
		li = list(set(M[0]))
		c = 0
		for i in li:
			rep = 0
			for j in M:
				if i in j:
					rep += 1
			if rep >= len(M):
				c += 1
		return c


class Solution:

	def distinct(self, M, N):
		h = {}
		for i in range(N):
			for j in range(N):
				if M[i][j] not in h:
					h[M[i][j]] = [i]
				elif h[M[i][j]][-1] != i:
					h[M[i][j]].append(i)
		ans = 0
		for i in h.keys():
			if len(h[i]) == N:
				ans += 1
		return ans


class Solution:

	def distinct(self, M, N):
		n = len(M)
		if n == 0:
			return 0
		freq = {}
		for element in M[0]:
			freq[element] = 1
		for i in range(1, n):
			new_freq = {}
			for element in M[i]:
				if element in freq and element not in new_freq:
					new_freq[element] = 1
			freq = new_freq
		count = len(freq)
		return count


class Solution:

	def distinct(self, M, N):
		dic = {}
		for i in range(0, N):
			if M[0][i] not in dic:
				dic[M[0][i]] = 1
		for i in range(1, N):
			for j in range(N):
				if M[i][j] in dic and dic[M[i][j]] == i:
					dic[M[i][j]] = dic[M[i][j]] + 1
		count = 0
		for i in dic:
			if dic[i] == N:
				count = count + 1
		return count


class Solution:

	def distinct(self, M, N):
		d = set(M[0])
		for i in range(1, N):
			d = d.intersection(set(M[i]))
		return len(d)


class Solution:

	def distinct(self, M, N):
		k = set(M[0])
		c = 0
		for i in k:
			v = True
			for j in M:
				if i not in j:
					v = False
			if v == True:
				c = c + 1
		return c


class Solution:

	def distinct(self, M, N):
		d = {}
		for i in range(N):
			d[M[0][i]] = 1
		for i in range(N):
			for j in range(N):
				if M[i][j] in d and d[M[i][j]] == i:
					d[M[i][j]] = i + 1
		c = 0
		for j in d.values():
			if j == N:
				c += 1
		return c


class Solution:

	def distinct(self, M, N):
		d = {}
		l = []
		c = 0
		for i in range(N):
			l.append(list(set(M[i])))
		for i in l:
			for j in i:
				if j not in d:
					d[j] = 1
				else:
					d[j] += 1
		for i in d:
			if d[i] == N:
				c += 1
		return c


class Solution:

	def distinct(self, M, N):
		res = set(M[0])
		for x in M:
			res = res & set(x)
		return len(res)


class Solution:

	def distinct(self, M, N):
		l = []
		for i in range(0, N):
			l.append(M[i])
		R = set(l[0])
		for i in range(1, N):
			R = R.intersection(set(l[i]))
		return len(R)


from collections import Counter

class Solution:

	def distinct(self, M, N):
		arr1 = []
		for i in M:
			temp = list(Counter(i).keys())
			arr1.extend(temp)
		arr2 = Counter(arr1)
		ans = list(filter(lambda x: x[1] == N, arr2.items()))
		return len(ans)


class Solution:

	def distinct(self, M, N):
		ans = set(M[0])
		for i in range(1, N):
			ans.intersection_update(set(M[i]))
		return len(ans)


class Solution:

	def distinct(self, m, n):
		return len(set(m[0]).intersection(*m))


class Solution:

	def distinct(self, m, n):
		v = set(m[0])
		for x in m[1:]:
			v = v.intersection(set(x))
		return len(v)


class Solution:

	def distinct(self, M, N):
		setti = set(M[0])
		for item in M:
			setti = setti.intersection(item)
		return len(setti)


class Solution:

	def distinct(self, M, N):
		l = []
		test = []
		count = 0
		for j in range(N):
			if M[0][j] not in test:
				test.append(M[0][j])
		for ele in test:
			f = True
			for j in range(N):
				if ele not in M[j]:
					f = False
			if f == True:
				count = count + 1
		return count


class Solution:

	def distinct(self, M, N):
		x = 0
		s = list()
		l = list()
		for i in range(len(M)):
			l.append(list(set(M[i])))
		for j in range(0, len(l[0])):
			for i in range(1, N):
				if l[0][j] not in l[i]:
					break
			else:
				x = x + 1
		return x


class Solution:

	def distinct(self, M, N):
		d = dict()
		count = 0
		for i in range(N):
			l = set(M[i])
			for j in l:
				if j not in d:
					d[j] = 1
				else:
					d[j] += 1
		for i in d:
			if d[i] == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		li = M[0]
		temp = []
		for i in range(N):
			counter = 0
			for j in range(N):
				if li[i] in M[j]:
					counter += 1
			if counter == N:
				temp.append(li[i])
		return len(set(temp))


class Solution:

	def distinct(self, M, N):
		from collections import defaultdict
		d = defaultdict(int)
		for i in range(N):
			t = set(M[i])
			for j in t:
				d[j] += 1
		c = 0
		for i in d:
			if d[i] == N:
				c += 1
		return c


class Solution:

	def distinct(self, M, N):
		x = list(set(M[0]))
		c = 0
		for i in x:
			y = 0
			for j in M:
				if i in j:
					y += 1
			if y >= N:
				c += 1
		return c


class Solution:

	def distinct(self, M, N):
		s1 = set(M[0])
		for i in range(N):
			s = set(M[i]) & s1
			s1 = s
		return len(list(s1))


class Solution:

	def distinct(self, M, N):

		def getMap(mx, olMap):
			amap = {}
			for i in mx:
				if olMap == None or olMap.get(i):
					amap[i] = 1
			return amap
		oldMap = None
		for r in M:
			newMap = getMap(r, oldMap)
			oldMap = newMap
		return len(oldMap.keys())


class Solution:

	def distinct(self, M, N):
		d = dict()
		num = []
		for i in range(N):
			if M[0][i] not in d:
				d[M[0][i]] = 0
		for i in range(N):
			dp = {}
			for j in range(N):
				if M[i][j] not in dp:
					dp[M[i][j]] = 1
			for key in dp.keys():
				if key in d:
					d[key] += 1
		count = 0
		for (k, v) in d.items():
			if v >= N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		value_count = 0
		r_dist = set(M[0])
		for i in r_dist:
			row_count = 1
			for j in range(1, N):
				if i in M[j]:
					row_count += 1
				else:
					break
			if row_count == N:
				value_count += 1
		return value_count


class Solution:

	def distinct(self, M, N):
		elems = {}
		elems[1] = set(M[0])
		for i in range(1, N):
			elems[i + 1] = set()
			for j in range(N):
				if M[i][j] in elems.get(i):
					elems[i + 1].add(M[i][j])
			if not elems[i + 1]:
				return 0
		return len(elems[N])


class Solution:

	def distinct(self, M, N):
		c = 0
		mp = dict()
		if N == 1:
			return 1
		else:
			for j in range(N):
				mp[M[0][j]] = 1
			for i in range(1, N):
				for j in range(N):
					if M[i][j] in mp.keys() and mp[M[i][j]] == i:
						mp[M[i][j]] = i + 1
						if i == N - 1:
							c += 1
			return c


from itertools import chain

class Solution:

	def distinct(self, M, N):
		dict = {}
		M = [set(x) for x in M]
		for x in M:
			for y in x:
				if y not in dict:
					dict[y] = 1
				else:
					dict[y] += 1
		count = 0
		for i in dict.values():
			if i == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		s = set()
		for i in range(N):
			s.add(M[0][i])
		for i in range(1, N):
			com = set()
			for j in range(N):
				com.add(M[i][j])
			s = s.intersection(com)
		return len(s)


class Solution:

	def distinct(self, M, N):
		h = {}
		count = 0
		for i in range(N):
			h[M[0][i]] = 1
		for i in range(N):
			for j in range(N):
				if M[i][j] in h and h[M[i][j]] == i:
					h[M[i][j]] = i + 1
		for i in h.values():
			if i == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		lst = list(set(M[0]))
		k = 0
		for i in lst:
			z = 0
			for j in M:
				if i in j:
					z += 1
			if z >= len(M):
				k += 1
		return k


class Solution:

	def distinct(self, M, N):
		d = {}
		ls = []
		count = 0
		for i in range(N):
			ls.extend(list(set(M[i])))
		for i in ls:
			if i not in d:
				d[i] = count + 1
			else:
				d[i] += 1
		for i in d.keys():
			if d[i] == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		k = {}
		ls = []
		for i in range(N):
			ls += list(set(M[i]))
		for x in ls:
			if x in k.keys():
				k[x] += 1
			else:
				k[x] = 1
		ct = 0
		for x in k:
			if k[x] == N:
				ct += 1
		return ct


class Solution:

	def distinct(self, M, N):
		s = {}
		for i in range(N):
			s[M[0][i]] = 1
		for i in range(1, N):
			m = {}
			for j in range(N):
				if M[i][j] in s:
					m[M[i][j]] = 1
			s = m
		return len(s)


import numpy as np

class Solution:

	def distinct(self, M, N):
		x = np.matrix(M)
		y = x.tolist()
		j = set(y[0])
		for i in range(1, len(y)):
			j = j.intersection(set(y[i]))
			if len(j) == 0:
				return 0
		return len(j)


class Solution:

	def distinct(self, M, N):
		hashmap = {}
		count = 0
		for i in range(N):
			hashmap[M[0][i]] = 1
		for i in range(1, N):
			for j in range(N):
				if M[i][j] in hashmap and hashmap[M[i][j]] == i:
					hashmap[M[i][j]] = i + 1
		for (k, v) in hashmap.items():
			if v == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		hashmap = {}
		for i in M:
			s = set(i)
			for j in s:
				if j in hashmap:
					hashmap[j] += 1
				else:
					hashmap[j] = 1
		count = 0
		for i in hashmap:
			if hashmap[i] == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		mp = dict()
		for items in M:
			S = list(set(items))
			for i in S:
				if i in mp:
					mp[i] += 1
				else:
					mp[i] = 1
		count = 0
		for item in mp.values():
			if item == N:
				count += 1
		return count


class Solution:

	def distinct(self, M, N):
		ans = []
		for i in M:
			ans.append(i)
		res = set(ans[0])
		for i in M:
			res = res.intersection(i)
		return len(res)


class Solution:

	def distinct(self, M, N):
		x = list(set(M[0]))
		y = []
		for i in range(len(x)):
			count = 0
			for j in range(N):
				if x[i] in M[j]:
					count += 1
			if count == N:
				y.append(x[i])
		return len(y)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = input().split()
		M = [[0] * N for i in range(N)]
		for itr in range(N * N):
			M[itr // N][itr % N] = int(arr[itr])
		ob = Solution()
		print(ob.distinct(M, N))


class Solution:

	def distinct(self, M, N):
		res = M[0]
		main_count = 0
		result = []
		for i in range(N):
			count = 0
			val = res[i]
			for j in range(N):
				if val in M[j]:
					count += 1
			if count >= N:
				if val not in result:
					result.append(val)
					main_count += 1
		return main_count
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = input().split()
		M = [[0] * N for i in range(N)]
		for itr in range(N * N):
			M[itr // N][itr % N] = int(arr[itr])
		ob = Solution()
		print(ob.distinct(M, N))


class Solution:

	def distinct(self, M, N):
		d = {}
		for i in M:
			for j in set(i):
				if j in d:
					d[j] += 1
				else:
					d[j] = 1
		c = 0
		for (i, a) in d.items():
			if a == N:
				c += 1
		return c
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = input().split()
		M = [[0] * N for i in range(N)]
		for itr in range(N * N):
			M[itr // N][itr % N] = int(arr[itr])
		ob = Solution()
		print(ob.distinct(M, N))


class Solution:

	def distinct(self, M, N):
		for i in range(N):
			if i > 0:
				if ln > 0:
					y = set(M[i])
					st = st.intersection(y)
					ln = len(st)
				else:
					break
			else:
				st = set(M[i])
				ln = len(st)
		return ln
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = input().split()
		M = [[0] * N for i in range(N)]
		for itr in range(N * N):
			M[itr // N][itr % N] = int(arr[itr])
		ob = Solution()
		print(ob.distinct(M, N))


class Solution:

	def distinct(self, M, N):
		ans = set(M[0])
		for i in M:
			ans = ans.intersection(set(i))
		return len(ans)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		arr = input().split()
		M = [[0] * N for i in range(N)]
		for itr in range(N * N):
			M[itr // N][itr % N] = int(arr[itr])
		ob = Solution()
		print(ob.distinct(M, N))