Given three arrays sorted in increasing order. Find the elements that are common in all three arrays.
Note: can you take care of the duplicates without using any additional Data Structure?
Example 1:
Input:
n1 = 6; A = {1, 5, 10, 20, 40, 80}
n2 = 5; B = {6, 7, 20, 80, 100}
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20 80
Explanation: 20 and 80 are the only
common elements in A, B and C.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function commonElements() which take the 3 arrays A[], B[], C[] and their respective sizes n1, n2 and n3 as inputs and returns an array containing the common element present in all the 3 arrays in sorted order. 
If there are no such elements return an empty array. In this case the output will be printed as -1.
 
Expected Time Complexity: O(n1 + n2 + n3)
Expected Auxiliary Space: O(n1 + n2 + n3)
 
Constraints:
1 <= n1, n2, n3 <= 10^5
The array elements can be both positive or negative integers.

Source: geeksforgeeks | Difficulty: EASY
Tags: Complete search

Starter code: #User function Template for python3

class Solution:
    def commonElements (self,A, B, C, n1, n2, n3):
        # your code here

Solutions:
class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		a.intersection_update(b)
		a.intersection_update(c)
		h = list(a)
		h.sort()
		return h


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = 0
		j = 0
		k = 0
		last = -12345678
		res = []
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] == C[k]:
				if last != A[i]:
					res.append(A[i])
					last = A[i]
				i += 1
				j += 1
				k += 1
			elif min(A[i], B[j], C[k]) == A[i]:
				i += 1
			elif min(A[i], B[j], C[k]) == B[j]:
				j += 1
			else:
				k += 1
		return res


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		z = set(A).intersection(set(B), set(C))
		res = list(z)
		res.sort()
		return res


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = 0
		j = 0
		st = []
		k = 0
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] == C[k]:
				if not st or st[-1] != A[i]:
					st.append(A[i])
				i += 1
				j += 1
				k += 1
			elif A[i] < B[j] or A[i] < C[k]:
				i += 1
			elif B[j] < C[k] or B[j] < A[i]:
				j += 1
			else:
				k += 1
		return st


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		ans = []
		(indexA, indexB, indexC, indexAns) = (0, 0, 0, 0)
		while indexA < n1 and indexB < n2 and (indexC < n3):
			if A[indexA] == B[indexB] and B[indexB] == C[indexC]:
				if A[indexA] not in ans:
					ans.append(A[indexA])
				indexA += 1
				indexB += 1
				indexC += 1
				continue
			maxval = max(A[indexA], B[indexB], C[indexC])
			while indexA < n1 and A[indexA] < maxval:
				indexA += 1
			while indexB < n2 and B[indexB] < maxval:
				indexB += 1
			while indexC < n3 and C[indexC] < maxval:
				indexC += 1
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		y = list(set(A) & set(B) & set(C))
		y.sort()
		return y


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		(i, j, k) = (0, 0, 0)
		ans = []
		for l in range(n1 + n2 + n3):
			if A[i] == B[j] and B[j] == C[k]:
				if len(ans) == 0 or ans[-1] != A[i]:
					ans.append(A[i])
			if i != n1 - 1 and A[i] <= B[j] and (A[i] <= C[k]):
				i += 1
			elif j != n2 - 1 and B[j] <= A[i] and (B[j] <= C[k]):
				j += 1
			elif k != n3 - 1:
				k += 1
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		ans = []
		(i, j, k) = (0, 0, 0)
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] == C[k]:
				if not ans:
					ans.append(A[i])
				elif ans[-1] != A[i]:
					ans.append(A[i])
				i += 1
				j += 1
				k += 1
			else:
				mxm = max(A[i], B[j], C[k])
				if A[i] < mxm:
					i += 1
				if B[j] < mxm:
					j += 1
				if C[k] < mxm:
					k += 1
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		(A, B, C) = (set(A), set(B), set(C))
		sol = A & B & C
		if sol:
			return sorted(list(sol))
		else:
			return [-1]


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		return sorted(list(set(A).intersection(set(B)).intersection(set(C))))


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = j = k = 0
		ans = []
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] and B[j] == C[k]:
				ans.append(A[i])
				i += 1
				j += 1
				k += 1
			elif A[i] < B[j]:
				i += 1
			elif B[j] < C[k]:
				j += 1
			else:
				k += 1
		if len(ans) == 0:
			return []
		final = [ans[0]]
		for i in range(1, len(ans)):
			if ans[i] == ans[i - 1]:
				continue
			else:
				final.append(ans[i])
		return final


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		sol_list = []
		(i, j, k) = (0, 0, 0)
		while True:
			if i == n1 or j == n2 or k == n3:
				break
			a = A[i]
			b = B[j]
			c = C[k]
			if a == b and b == c:
				sol_list.append(b)
			if a <= b:
				if a <= c:
					i += 1
				else:
					k += 1
			elif b <= c:
				j += 1
			else:
				k += 1
		return sorted(list(set(sol_list)))


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		x = []
		i = j = k = 0
		c = None
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] == C[k] and c != A[i]:
				c = A[i]
				x.append(A[i])
				i += 1
				j += 1
				k += 1
			elif A[i] < B[j]:
				i += 1
			elif C[k] > B[j]:
				j += 1
			else:
				k += 1
		return x


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = j = k = 0
		common_elements = []
		while i < len(A) and j < len(B) and (k < len(C)):
			if A[i] == B[j] == C[k]:
				if A[i] not in common_elements:
					common_elements.append(A[i])
				i += 1
				j += 1
				k += 1
			elif A[i] < B[j]:
				i += 1
			elif B[j] < C[k]:
				j += 1
			else:
				k += 1
		return common_elements


class Solution:

	def commonElements(self, a, b, c, n1, n2, n3):
		a = set(a)
		b = set(b)
		c = set(c)
		res = []
		for i in a:
			if i in b and i in c:
				res.append(i)
		return sorted(res)


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		(i, j, k) = (0, 0, 0)
		res = []
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] and B[j] == C[k]:
				res.append(A[i])
				i += 1
				j += 1
				k += 1
			elif A[i] < B[j]:
				i += 1
			elif B[j] < C[k]:
				j += 1
			else:
				k += 1
		ans = set(res)
		x = list(ans)
		x.sort()
		return x


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		v = []
		mapa = {}
		mapb = {}
		mapc = {}
		for i in range(n1):
			mapa[A[i]] = 1
		for i in range(n2):
			mapb[B[i]] = 1
		for i in range(n3):
			mapc[C[i]] = 1
		for i in range(n1):
			if A[i] in mapb.keys() and A[i] in mapc.keys():
				v.append(A[i])
				del mapa[A[i]]
				del mapb[A[i]]
				del mapc[A[i]]
		return v


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		comm_abc = list(a.intersection(b).intersection(c))
		return sorted(comm_abc)


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		r1 = a.intersection(b)
		r2 = r1.intersection(c)
		lst = list(r2)
		lst.sort()
		return lst


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		A = set(A)
		B = set(B)
		C = set(C)
		res = sorted(A & B & C)
		return res


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		set_A = set(A)
		set_B = set(B)
		set_C = set(C)
		common_elements = set_A.intersection(set_B, set_C)
		return sorted(common_elements)


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		A = set(A)
		B = set(B)
		C = set(C)
		lis = []
		for i in A:
			if i in B and i in C:
				lis.append(i)
		lis.sort()
		return lis


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		A1 = set(A)
		B1 = set(B)
		C1 = set(C)
		common = A1.intersection(B1, C1)
		d = list(common)
		d.sort()
		return d


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		x = set(A)
		y = set(B)
		z = set(C)
		p = x.intersection(y)
		q = p.intersection(z)
		b = list(q)
		b.sort()
		return b


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		(A, B, C) = (set(A), set(B), set(C))
		v = A.intersection(B)
		k = list(v.intersection(C))
		k.sort()
		return k


class Solution:

	def commonElements(self, a, b, c, n1, n2, n3):
		return sorted(list(set(a) & set(b) & set(c)))


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		cab = a.intersection(b)
		cabc = cab.intersection(c)
		lst = list(cabc)
		lst.sort()
		return lst


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		ab = a.intersection(b)
		abc = ab.intersection(c)
		l = list(abc)
		l.sort()
		return l


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		A = set(A)
		B = set(B)
		C = set(C)
		t = A.intersection(B)
		r = list(t.intersection(C))
		r.sort()
		return r


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		t = a.intersection(b)
		t1 = t.intersection(c)
		t1 = list(t1)
		t1.sort()
		return t1


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		p1 = 0
		p2 = 0
		p3 = 0
		ans = []
		curr_min = 9999
		while p1 < n1 and p2 < n2 and (p3 < n3):
			curr_min = min(min(A[p1], B[p2]), C[p3])
			if A[p1] == B[p2] == C[p3]:
				if len(ans) != 0 and ans[len(ans) - 1] != A[p1]:
					ans.append(A[p1])
				if len(ans) == 0:
					ans.append(A[p1])
				p1 += 1
				p2 += 1
				p3 += 1
			elif A[p1] == curr_min:
				p1 += 1
			elif B[p2] == curr_min:
				p2 += 1
			else:
				p3 += 1
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = len(A)
		b = len(B)
		c = len(C)
		output = []
		A = set(A)
		B = set(B)
		C = set(C)
		t = A.intersection(B)
		r = t.intersection(C)
		output = list(r)
		output.sort()
		return output


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		(i, j, k) = (0, 0, 0)
		common = []
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] and B[j] == C[k]:
				common.append(A[i])
				i += 1
				j += 1
				k += 1
			elif A[i] < B[j]:
				i += 1
			elif B[j] < C[k]:
				j += 1
			else:
				k += 1
		if common:
			last = common[0]
			result = [last]
			for num in common[1:]:
				if num != last:
					result.append(num)
					last = num
			return result
		else:
			return [-1]


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		uset1 = set()
		uset2 = set()
		uset3 = []
		for i in range(n1):
			uset1.add(A[i])
		for j in range(n2):
			uset2.add(B[j])
		for i in range(n3):
			if C[i] in uset1 and C[i] in uset2:
				if C[i] not in uset3:
					uset3.append(C[i])
		return uset3


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		b = []
		a = []
		A = set(A)
		B = set(B)
		C = set(C)
		a.append(A.intersection(B, C))
		a = a[0]
		r = list(a)
		r.sort()
		return r


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		A_dict = {}
		B_dict = {}
		C_dict = {}
		common_ele = []
		common_dict = {}
		for i in range(n1):
			if A[i] in A_dict:
				A_dict[A[i]] += 1
			else:
				A_dict[A[i]] = 1
		for i in range(n2):
			if B[i] in B_dict:
				B_dict[B[i]] += 1
			else:
				B_dict[B[i]] = 1
		for i in range(n3):
			if C[i] in C_dict:
				C_dict[C[i]] += 1
			else:
				C_dict[C[i]] = 1
		for i in range(n1):
			if A[i] in B_dict and A[i] in C_dict and (A[i] not in common_dict):
				common_ele.append(A[i])
				common_dict[A[i]] = 1
		return common_ele


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		d = {}
		D = [set(A), set(B), set(C)]
		for j in D:
			for i in j:
				try:
					d[i] = d[i] + 1
				except KeyError:
					d[i] = 1
		D = []
		s = []
		for i in d.keys():
			s.append(i)
		s.sort()
		for i in s:
			if d[i] == 3:
				D.append(i)
		return D


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		SET = set(A)
		BSET = SET.intersection(set(B), set(C))
		listwa = list(BSET)
		listwa.sort()
		return listwa


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		ans = []
		map1 = {}
		for i in range(n1):
			map1[A[i]] = True
		map2 = {}
		for i in range(n2):
			map2[B[i]] = True
		map3 = {}
		for i in range(n3):
			map3[C[i]] = True
		for ele in map1:
			if ele in map2 and ele in map3:
				ans.append(ele)
		ans.sort()
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		set_a = set(A)
		set_b = set(B)
		set_c = set(C)
		count = set_a.intersection(set_b)
		counter = count.intersection(set_c)
		return sorted(counter)


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		A = set(A)
		B = set(B)
		C = set(C)
		res1 = A.intersection(B)
		res2 = res1.intersection(C)
		lst = list(res2)
		lst.sort()
		return lst


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		dict = {}
		farr = []
		arr = []
		for i in A:
			dict.update({i: 1})
		for i in B:
			if dict.get(i) != None:
				dict.update({i: 2})
		for i in C:
			if dict.get(i) == 2:
				farr.append(i)
				dict.update({i: 3})
		return farr


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		s1 = set(A)
		s2 = set(B)
		s3 = set(C)
		s = s1 & s2 & s3
		l = sorted(list(s))
		return l


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		common = set()
		aSet = set(A)
		for i in B:
			if i in aSet:
				common.add(i)
		res = []
		for i in C:
			if i in common:
				if not res or i != res[-1]:
					res.append(i)
		return res


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		common = set()
		A_set = set(A)
		for num in B:
			if num in A_set:
				common.add(num)
		result = []
		for num in C:
			if num in common:
				if not result or num != result[-1]:
					result.append(num)
		return result


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		x = a.intersection(b)
		y = x.intersection(c)
		l = list(y)
		l.sort()
		return l


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		set_B = set(B)
		common_AB = set([x for x in A if x in set_B])
		set_lis = set()
		ans = set()
		for i in range(n3):
			if C[i] in common_AB:
				if C[i] not in set_lis:
					set_lis.add(C[i])
					ans.add(C[i])
		ans = list(ans)
		ans.sort()
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		x = set(A)
		y = set(B)
		z = set(C)
		l = x.intersection(y)
		ans = list(l.intersection(z))
		ans.sort()
		return ans


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		s1 = set(A)
		s2 = set(B)
		val = []
		for i in range(n3):
			if C[i] in s1 and C[i] in s2:
				if C[i] not in val:
					val.append(C[i])
		return val


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = 0
		j = 0
		k = 0
		result = []
		last_element = None
		while i < n1 and j < n2 and (k < n3):
			if A[i] == B[j] == C[k]:
				if A[i] != last_element:
					result.append(A[i])
					last_element = A[i]
				i += 1
				j += 1
				k += 1
			elif A[i] <= B[j] and A[i] <= C[k]:
				i += 1
			elif B[j] <= A[i] and B[j] <= C[k]:
				j += 1
			else:
				k += 1
		if len(result) == 0:
			return [-1]
		else:
			return result


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		lst = []
		lst.extend(set(A) & set(B) & set(C))
		if lst == []:
			return [-1]
		else:
			return sorted(lst)


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		car = a.intersection(b)
		cars = car.intersection(c)
		l = list(cars)
		l.sort()
		return l


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = 0
		j = 0
		k = 0
		res = set()
		while i < n1 and j < n2 and (k < n3):
			if A[i] < B[j]:
				i += 1
			elif B[j] < C[k]:
				j += 1
			elif C[k] < A[i] or C[k] < B[j]:
				k += 1
			else:
				res.add(A[i])
				i += 1
				j += 1
				k += 1
		res = list(res)
		res.sort()
		return res


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		i = 0
		j = 0
		res = {}
		while i < n1 and j < n2:
			if A[i] > B[j]:
				j += 1
			elif A[i] < B[j]:
				i += 1
			else:
				if A[i] not in res:
					res[A[i]] = 1
				i += 1
				j += 1
		if len(res) == 0:
			return [-1]
		k = 0
		while k < n3:
			if C[k] in res:
				res[C[k]] = 0
			k += 1
		res = [x for x in res if res[x] == 0]
		res.sort()
		return res


class Solution:

	def commonElements(self, A, B, C, n1, n2, n3):
		a = set(A)
		b = set(B)
		c = set(C)
		j = a.intersection(b)
		k = j.intersection(c)
		result = list(k)
		result.sort()
		return result