As you might remember, the collector of Siruseri had ordered
a complete revision of the Voters List. He knew that constructing
the list of voters is a difficult task, prone to errors. Some
voters may have been away on vacation, others may have moved
during the enrollment and so on. 

To be as accurate as possible, he entrusted the task to three different 
officials. Each of them was to independently record the list of voters and 
send it to the collector. In Siruseri, every one has a ID number and
the list would only list the ID numbers of the voters and not their names.
The officials were expected to arrange the ID numbers in ascending order
in their lists. 

On receiving the lists, the Collector realised that there were
discrepancies - the three lists were not identical.  He decided
to go with the majority. That is, he decided to construct the
final list including only those ID numbers that appeared in at
least 2 out of the 3 lists.  For example if the three lists
were

23  30  42  57  90
21  23  35  57  90  92
21  23  30  57  90 

then the final list compiled by the collector would be:

21  23  30  57  90

The ID numbers 35, 42 and 92 which appeared in only one list
each do not figure in the final list.

Your task is to help the collector by writing a program that
produces the final list from the three given lists.

Input format

The first line of the input contains 3 integers
N_{1}, N_{2} and
N_{3}.  N_{1} is the number of
voters in the first list, N_{2} is the number of
voters in the second list and N_{3} is the number of
voters in the third list.  The next N_{1} lines
(lines 2,...,N_{1}+1) contain one positive integer
each and describe the first list in ascending order.  The following

N_{2} lines (lines
N_{1}+2,...,N_{1}+N_{2}+1)
describe the second list in ascending order and the final
N_{3} lines (lines

N_{1}+N_{2}+2,...,N_{1}+N_{2}+N_{3}+1)
describe the third list in ascending order.

Output format

The first line of the output should contain a single integer
M indicating the number voters in the final list. The next
M lines (lines 2,...,M+1) should contain one
positive integer each, describing the list of voters in the final
list, in ascending order.

Test data

You may assume that 1 ≤
N_{1},N_{2},N_{3}
≤ 50000.

----- Sample Input 1 ------ 
5 6 5
23
30
42
57
90
21 
23 
35 
57 
90 
92 
21 
23 
30 
57 
90 
----- Sample Output 1 ------ 
5
21 
23 
30 
57 
90

Source: codechef | Difficulty: EASY
Tags: Data structures

Implementation


Solutions:
(n1, n2, n3) = map(int, input().split())
myDict = {}
outputSet = set()
count = 0
for i in range(n1 + n2 + n3):
	num = int(input())
	if num in myDict:
		myDict[num] += 1
		if myDict[num] > 1:
			outputSet.add(num)
			count += 1
	else:
		myDict[num] = 1
print(len(outputSet))
for i in sorted(outputSet):
	print(i)


n = sum(map(int, input().split()[:3]))
l = []
m = []
k = 1
for i in range(n):
	l.append(int(input()))
l.sort()
for i in range(n - 1):
	if l[i] == l[i + 1]:
		k = k + 1
	else:
		k = 1
	if k == 2:
		m.append(l[i])
		k = 0
print(len(m))
for i in range(len(m)):
	print(m[i])


(a, b, c) = map(int, input().split())
(a_list, b_list, c_list, result) = ([], [], [], [])
for x in range(a):
	a_list.append(int(input()))
for x in range(b):
	b_list.append(int(input()))
for x in range(c):
	c_list.append(int(input()))
result.extend(list(set(a_list).intersection(set(b_list))))
result.extend(list(set(a_list).intersection(set(c_list))))
result.extend(list(set(b_list).intersection(set(c_list))))
print(len(list(set(result))))
for x in sorted(list(set(result))):
	print(x)


(N1, N2, N3) = map(int, input().split())
list_1 = []
final_list = []
for i in range(N1):
	list_1.append(int(input()))
for i in range(N2):
	list_1.append(int(input()))
for i in range(N3):
	list_1.append(int(input()))
list_1.sort()
count = 1
for i in range(len(list_1) - 1):
	if count == 2:
		final_list.append(list_1[i])
	if list_1[i] == list_1[i + 1]:
		count += 1
	else:
		count = 1
print(len(final_list))
for i in final_list:
	print(i)


(n1, n2, n3) = map(int, input().split())
(l1, l2, l3) = ([], [], [])
(s1, s2, s3) = ({}, {}, {})
for i in range(n1):
	l1.append(int(input()))
for i in range(n2):
	l2.append(int(input()))
for i in range(n3):
	l3.append(int(input()))
l1 = set(l1)
l2 = set(l2)
l3 = set(l3)
s1 = l1.intersection(l2)
s2 = l1.intersection(l3)
s3 = l2.intersection(l3)
s = sorted(s1.union(s2, s3))
print(len(s))
for _ in s:
	print(_)


d = {}
arr = []
(n1, n2, n3) = map(int, input().split())
a = n1 + n2 + n3
for i in range(a):
	x = int(input())
	d[x] = d.get(x, 0) + 1
for x in d:
	if d.get(x) >= 2:
		arr.append(x)
print(len(arr))
for x in sorted(arr):
	print(x)


(a, b, c) = list(map(int, input().split()))
d = {}
e = []
x = 0
for i in range(a + b + c):
	n = int(input())
	if n in d:
		d[n] = d[n] + 1
		if d[n] == 2:
			x = x + 1
			e.append(n)
	else:
		d[n] = 1
print(x)
e.sort()
for key in e:
	print(key)


from collections import Counter
(n1, n2, n3) = map(int, input().split())
l = list()
lp = list()
for i in range(n1):
	l1 = int(input())
	l.append(l1)
for i in range(n2):
	l2 = int(input())
	l.append(l2)
for i in range(n3):
	l3 = int(input())
	l.append(l3)
l.sort()
y = Counter(l)
for d in y:
	if y.get(d) >= 2:
		lp.append(d)
print(len(lp))
for x in lp:
	print(x)


(n1, n2, n3) = map(int, input().split())
l = {}
l1 = []
for i in range(n1 + n2 + n3):
	n = int(input())
	if n not in l:
		l[n] = 1
	else:
		l[n] += 1
for ele in l:
	if l[ele] >= 2:
		l1.append(ele)
l1.sort()
print(len(l1))
for item in l1:
	print(item)


from collections import Counter
(n1, n2, n3) = map(int, input().split())
a = []
c = []
sumi = n1 + n2 + n3
for i in range(sumi):
	a.append(int(input()))
a.sort()
b = Counter(a)
for (i, j) in b.items():
	if j > 1:
		c.append(i)
print(len(c))
for i in c:
	print(i)


(a, b, c) = map(int, input().split())
a1 = []
a2 = []
a3 = []
while a:
	n = int(input())
	a1.append(n)
	a = a - 1
while b:
	m = int(input())
	a2.append(m)
	b = b - 1
while c:
	z = int(input())
	a3.append(z)
	c = c - 1
k = a1 + a2 + a3
d = {}
for i in k:
	if i not in d:
		d[i] = 1
	else:
		d[i] += 1
o = []
for (i, j) in d.items():
	if j > 1:
		o.append(i)
print(len(o))
o = sorted(o)
for i in o:
	print(i)


(N1, N2, N3) = map(int, input().split())
D = []
for i in range(N1):
	x = int(input())
	D.append(x)
for i in range(N2):
	y = int(input())
	D.append(y)
for i in range(N3):
	z = int(input())
	D.append(z)
(d, l) = ({}, [])
for i in D:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
for (key, value) in d.items():
	if value >= 2:
		l.append(key)
l.sort()
print(len(l))
for i in l:
	print(i)


from collections import Counter
(n1, n2, n3) = map(int, input().split(' '))
lis = []
count = 0
for i in range(n1 + n2 + n3):
	lis.append(int(input()))
lis.sort()
counter = Counter(lis)
for (key, value) in counter.items():
	if value >= 2:
		count += 1
print(count)
for (key, value) in counter.items():
	if value >= 2:
		print(key)


d = {}
list = []
(a, b, c) = map(int, input().split())
sum = a + b + c
for i in range(sum):
	x = int(input())
	d[x] = d.get(x, 0) + 1
for x in d:
	if d.get(x) >= 2:
		list.append(x)
print(len(list))
for x in sorted(list):
	print(x)


(N1, N2, N3) = map(int, input().split())
A1 = set()
A2 = set()
A3 = set()
for i in range(N1):
	A1.add(int(input()))
for i in range(N2):
	A2.add(int(input()))
for i in range(N3):
	A3.add(int(input()))
A = A1 | A2 | A3
voters = 0
B = []
for a in A:
	count = (a in A1) + (a in A2) + (a in A3)
	if count > 1:
		voters += 1
		B.append(a)
print(voters)
B.sort()
for b in B:
	print(b)


from collections import Counter
(a, b, c) = map(int, input().split())
s = a + b + c
l = []
l1 = []
for i in range(s):
	l.append(int(input()))
k = Counter(l)
for i in k:
	if k[i] >= 2:
		l1.append(i)
print(len(l1))
for i in sorted(l1):
	print(i)


(n1, n2, n3) = map(int, input().split())
lst = {}
for i in range(n1 + n2 + n3):
	n = int(input())
	if n not in lst:
		lst[n] = 1
	else:
		lst[n] += 1
res = []
for i in lst:
	if lst[i] >= 2:
		res.append(i)
res.sort()
print(len(res))
for i in res:
	print(i)


(a, b, c) = map(int, input().split(' '))
lst = []
lst_new = set()
for i in range(a + b + c):
	a = int(input())
	lst.append(a)
lst.sort()
for j in range(len(lst) - 1):
	if lst[j] == lst[j + 1]:
		lst_new.add(lst[j])
print(len(lst_new))
print(*sorted(lst_new), sep='\n')


(n1, n2, n3) = map(int, input().split())
a = []
d = {}
for i in range(n1 + n2 + n3):
	n = int(input())
	if n not in d:
		d[n] = 0
	d[n] += 1
for item in d:
	if d[item] > 1:
		a.append(item)
a.sort()
print(len(a))
for item in a:
	print(item)


(one, two, three) = map(int, input().split())
listone = []
listtwo = []
listthree = []
res = []
for i in range(one):
	listone.append(int(input()))
for j in range(two):
	listtwo.append(int(input()))
for k in range(three):
	listthree.append(int(input()))
counts = dict()
for i in listone:
	if i not in counts:
		counts[i] = 1
	else:
		counts[i] += 1
for i in listtwo:
	if i not in counts:
		counts[i] = 1
	else:
		counts[i] += 1
for i in listthree:
	if i not in counts:
		counts[i] = 1
	else:
		counts[i] += 1
for i in counts:
	if counts[i] >= 2:
		res.append(i)
print(len(res))
res.sort()
for i in res:
	print(i)


from collections import Counter
(n1, n2, n3) = map(int, input().split())
a = []
r = []
for i in range(n1 + n2 + n3):
	a.append(int(input()))
x = Counter(a)
for ele in x:
	if x[ele] >= 2:
		r.append(ele)
print(len(r))
r = sorted(r)
for ele in r:
	print(ele)


d = {}
list = []
(n1, n2, n3) = map(int, input().split())
sum = n1 + n2 + n3
for i in range(sum):
	x = int(input())
	d[x] = d.get(x, 0) + 1
for x in d:
	if d.get(x) >= 2:
		list.append(x)
print(len(list))
for x in sorted(list):
	print(x)


(n1, n2, n3) = map(int, input().split())
count = dict()
for i in range(n1 + n2 + n3):
	num = int(input())
	if count.get(num) is not None:
		count.update({num: count.get(num) + 1})
	else:
		count.update({num: 1})
lst = []
for val in count:
	if count[val] >= 2:
		lst.append(val)
lst.sort()
print(len(lst))
for i in lst:
	print(i)


(n1, n2, n3) = map(int, input().split())
d = dict()
l = []
for i in range(n1 + n2 + n3):
	x = int(input())
	if x not in d:
		d[x] = 1
	else:
		d[x] += 1
for x in d:
	if d[x] > 1:
		l.append(x)
print(len(l))
l.sort()
for i in l:
	print(i)


from collections import Counter
(a, b, c) = map(int, input().split())
u = []
o = []
for i in range(a):
	s = int(input())
	u.append(s)
for j in range(b):
	r = int(input())
	u.append(r)
for k in range(c):
	p = int(input())
	u.append(p)
k = Counter(u)
for i in k:
	if k[i] >= 2:
		o.append(i)
print(len(o))
for j in sorted(o):
	print(j)


from collections import Counter
(n1, n2, n3) = map(int, input().split())
s1 = []
for j in range(n1):
	x = int(input())
	s1.append(x)
for k in range(n2):
	y = int(input())
	s1.append(y)
for l in range(n3):
	z = int(input())
	s1.append(z)
s1.sort()
m = Counter(s1)
s1.clear()
for i in m:
	if m[i] >= 2:
		s1.append(i)
print(len(s1))
for i in s1:
	print(i)


from collections import Counter
(n1, n2, n3) = list(map(int, input().split()))
lst = []
for i in range(n1):
	e = int(input())
	lst.append(e)
for i in range(n2):
	e = int(input())
	lst.append(e)
for i in range(n3):
	e = int(input())
	lst.append(e)
lst.sort()
k = Counter(lst)
lst.clear()
for i in k:
	if k[i] >= 2:
		lst.append(i)
print(len(lst))
for i in lst:
	print(i)


def voters_list():
	ans = []
	hash = [0] * (max(voters) + 10)
	for i in voters:
		hash[i] += 1
		if hash[i] >= 2:
			ans.append(i)
	ans = list(set(ans))
	ans.sort()
	print(len(ans))
	for i in ans:
		print(i)
(n1, n2, n3) = map(int, input().split())
voters = []
for i in range(n1 + n2 + n3):
	voters.append(int(input()))
voters_list()


(x, y, z) = map(int, input().split())
a = []
b = []
c = []
for i in range(x):
	a.append(int(input()))
for i in range(y):
	b.append(int(input()))
for i in range(z):
	c.append(int(input()))
a = set(a)
b = set(b)
c = set(c)
d = a.intersection(b).union(a.intersection(c)).union(c.intersection(b)).union(a.intersection(b.intersection(c)))
d = list(d)
d.sort()
print(len(d))
for i in range(len(d)):
	print(d[i])


from collections import Counter
(l, m, n) = map(int, input().split())
b = l + m + n
l = []
l1 = []
for i in range(b):
	l.append(int(input()))
k = Counter(l)
for i in k:
	if k[i] >= 2:
		l1.append(i)
print(len(l1))
for i in sorted(l1):
	print(i)


[n1, n2, n3] = list(map(int, input().split()))
d = {}
for i in range(n1 + n2 + n3):
	k = int(input())
	d[k] = d.get(k, 0) + 1
l = []
for i in d.keys():
	if d[i] >= 2:
		l.append(i)
l.sort()
print(len(l))
for i in l:
	print(i)


n = sum(map(int, input().split()))
a = []
for i in range(n):
	a.append(int(input()))
a.sort()
(cnt, res) = (1, [])
for i in range(n - 1):
	if a[i] == a[i + 1]:
		cnt += 1
	else:
		cnt = 1
	if cnt == 2:
		res.append(a[i])
		cnt = 0
print(len(res))
for i in res:
	print(i)


(n1, n2, n3) = map(int, input().split())
s = n1 + n2 + n3
d = {}
for i in range(s):
	ele = int(input())
	if ele in d.keys():
		d[ele] = d[ele] + 1
	else:
		d.update({ele: 1})
L = []
for i in d:
	if d[i] >= 2:
		L.append(i)
L.sort()
print(len(L))
for i in L:
	print(i)


(N1, N2, N3) = map(int, input().split())
a = []
b = []
c = []
for i in range(N1):
	a.append(int(input()))
for i in range(N2):
	b.append(int(input()))
for i in range(N3):
	c.append(int(input()))
N1 = sorted(set(a) & set(b) | set(b) & set(c) | set(a) & set(c))
print(len(N1))
for i in N1:
	print(i)


(n1, n2, n3) = map(int, input().split())
l1 = list((int(input()) for n11 in range(n1)))
l2 = list((int(input()) for n22 in range(n2)))
l3 = list((int(input()) for n33 in range(n3)))
common12 = set(l1).intersection(l2)
common13 = set(l1).intersection(l3)
common23 = set(l2).intersection(l3)
common = set.union(common12, common13, common23)
print(len(common))
z = [print(x) for x in sorted(common)]


from collections import Counter
(x, y, z) = map(int, input().split())
l = []
m = []
a = x + y + z
for i in range(a):
	b = int(input())
	l.append(b)
c = Counter(l)
for i in c:
	if c[i] >= 2:
		m.append(i)
print(len(m))
m = sorted(m)
for i in m:
	print(i)


(l1, l2, l3) = map(int, input().split())
(s1, s2, s3) = ([], [], [])
for i in range(l1):
	l1i = int(input())
	s1.append(l1i)
for i in range(l2):
	l2i = int(input())
	s2.append(l2i)
for i in range(l3):
	l3i = int(input())
	s3.append(l3i)
ss1 = set(s1).intersection(set(s2))
ss2 = set(s2).intersection(set(s3))
ss3 = set(s3).intersection(set(s1))
(ss1, ss2, ss3) = (list(ss1), list(ss2), list(ss3))
final = []
final = ss1 + ss2 + ss3
final = set(final)
final = list(final)
final.sort()
print(len(final))
for i in final:
	print(i)


(x, y, z) = map(int, input().split())
a = []
b = []
c = []
for i in range(x):
	a.append(int(input()))
for i in range(y):
	b.append(int(input()))
for i in range(z):
	c.append(int(input()))
x = sorted(set(a) & set(b) | set(b) & set(c) | set(a) & set(c))
print(len(x))
for i in x:
	print(i)


d = {}
(a, s, l) = map(int, input().split())
for _ in range(a + s + l):
	n = int(input())
	if n not in d:
		d[n] = 1
	else:
		d[n] += 1
c = 0
l = []
for i in d:
	if d[i] > 1:
		l.append(i)
		c += 1
print(c)
l = sorted(l)
for i in l:
	print(i)


(a, b, c) = map(int, input().split())
d = dict()
for i in range(a + b + c):
	n = int(input())
	if n not in d:
		d[n] = 1
	else:
		d[n] += 1
l = []
for i in d:
	if d[i] > 1:
		l.append(i)
print(len(l))
l.sort()
for i in l:
	print(i)


d = {}
l = []
(n1, n2, n3) = map(int, input().split())
s = n1 + n2 + n3
for i in range(s):
	x = int(input())
	d[x] = d.get(x, 0) + 1
for x in d:
	if d.get(x) >= 2:
		l.append(x)
print(len(l))
for x in sorted(l):
	print(x)