Problem:

Set Intersection

  Find the intersection of two sets $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$.

Input

  The input is given in the following format.

$n$
$a_0 \; a_1 \; ... \; a_{n-1}$
$m$
$b_0 \; b_1 \; ... \; b_{m-1}$

  Elements of $A$ and $B$ are given in ascending order respectively. There are no duplicate elements in each set.

Output

  Print elements in the intersection in ascending order. Print an element in a line.
 

Constraints

  
$1 \leq n, m \leq 200,000$

    
$0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$

  
$0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$

Sample Input 1

4
1 2 5 8
5
2 3 5 9 11

Sample Output 1

2
5



Sample input:
4
1 2 5 8
5
2 3 5 9 11


Sample output:
2
5

Submission status: Accepted

Solution code:
input()
a = set(list(map(int,input().split())))
input()
b = set(list(map(int,input().split())))

for i in sorted(a&b):
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
a = set(map(int, input().split()))
m = int(input())
b = set(map(int, input().split()))
for i in sorted(a & b):
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
a = set(map(int,input().split()))
m = int(input())
b = set(map(int,input().split()))
c = a & b
if c:
    print(*sorted(c),sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
a = set(map(int, input().split()))
m = int(input())
b = set(map(int, input().split()))
s_intersection = a.intersection(b)
if s_intersection:
    print(*sorted(s_intersection), sep="\n")

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = int(input())
    b = list(map(int,input().split()))
    s = sorted(set(a)&set(b))
    for c in s:print (c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = set(list(map(int, input().split())))
m = int(input())
B = set(list(map(int, input().split())))

answer = A.intersection(B)
[print(i) for i in sorted(answer)]

Submission status: Accepted

Solution code:
a = input()
setA = set(map(int, input().split()))
b = input()
setB = set(map(int, input().split()))

for elem in sorted(setA.intersection(setB)):
    print(elem)

Submission status: Accepted

Solution code:
input()
a = set(map(int, input().split()))
input()
b = set(map(int, input().split()))
[print(s) for s in map(str, sorted(list(a.intersection(b))))]

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = set([int(a) for a in input().split()])
    m = int(input())
    b = set([int(a) for a in input().split()])

    c = a.intersection(b)
    sorted_c = sorted(c)
    for i in sorted_c:
        print(i)
main()

Submission status: Accepted

Solution code:
n=input()
a=set(map(int, input().split()))
m=input()
b=set(map(int, input().split()))
c={print(x) for x in sorted(set(a&b))}

Submission status: Accepted

Solution code:
input()
A = [int(x) for x in input().split() ]
input()
B = [int(x) for x in input().split() ]

for i in sorted(set(A)&set(B)):
    print(i)

Submission status: Accepted

Solution code:
"""
2つの集合から積集合を求める

input:
4
2 3 5 6
5
2 5 6 7 9
output:
2
5
6
"""

if __name__ == "__main__":
    input()
    A = set(map(int, input().split()))
    input()
    B = set(map(int, input().split()))
    I = A & B
    if I:
        print(*sorted(I), sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
a = set(map(int, input().split()))
m = int(input())
b = set(map(int, input().split()))

for n in sorted(a & b): print(n)

Submission status: Accepted

Solution code:
n = int(input())
a = set(map(int, input().split( )))
m = int(input())
b = set(map(int, input().split( )))
sct = a&b

ans = list(sct)
ans.sort()

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    a = set(map(lambda x: int(x), input().split()))
    m = int(input())
    b = set(map(lambda x: int(x), input().split()))

    for elem in sorted(a.intersection(b)):
        print(elem)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Set Operation - Set Intersection
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_9_B&lang=jp

"""
_ = input()
A = set([int(a) for a in input().split()])
_ = input()
B = set([int(b) for b in input().split()])
ans = sorted(A & B)
if ans:
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
A = set(map(int, input().split()))
M = int(input())
B = set(map(int, input().split()))
C = A & B
if C:
    print(*sorted(C), sep='\n')

Submission status: Accepted

Solution code:
if __name__ == '__main__':

	input()
	A = set(map(int,input().split()))
	input()
	B = set(map(int,input().split()))

	C = sorted(A&B)

	for i in C:
		print(i)

Submission status: Accepted

Solution code:
n=int(input())
a=set(map(int,input().split()))
m=int(input())
b=set(map(int,input().split()))
if a&b:
  print(*sorted(a&b),sep='\n')

Submission status: Accepted

Solution code:
n = int(input())

A = set(map(int, input().split()))

m = int(input())

B = set(map(int, input().split()))

for c in sorted(list(A & B)):
    print(c)

Submission status: Accepted

Solution code:
input()
a = set(list(map(int,input().split())))
input()
b = set(list(map(int,input().split())))
ans = a&b
ans = sorted(list(ans))
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
A = set(map(int, input().split()))
m = int(input())
B = set(map(int, input().split()))
C = A&B
if C:
    print(*sorted(C), sep='\n')

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))

m=int(input())
B=list(map(int,input().split()))

C=list(set(A)&set(B))
C.sort()

if C!=[]:
    print('\n'.join(map(str,C)))

Submission status: Accepted

Solution code:
input()
A = set(int(x) for x in input().split())
input()
B = set(int(x) for x in input().split())

for i in sorted(A & B):
    print(i)

Submission status: Accepted

Solution code:
input()
s1 = set(map(int, input().split()))
input()
s2 = set(map(int, input().split()))

s = sorted(list(s1.intersection(s2)))

if len(s) != 0:
    print('\n'.join(map(str, s)))

Submission status: Accepted

Solution code:
n = int(input())
alist = list(map(int, input().split()))
m = int(input())
blist = list(map(int, input().split()))

anslist = set(alist) & set(blist)
anslist = list(anslist)
anslist.sort()

for ans in anslist:
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = set(map(int, input().split()))
M = int(input())
B = set(map(int, input().split()))
C = A & B
if C:
    print(*sorted(C), sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# ITP2_9_B: Set Operation - Set Intersection


def intersect(n, m, a, b):
    i, j = 0, 0
    while i < n and j < m:
        if a[i] < b[j]:
            i += 1
        elif a[i] > b[j]:
            j += 1
        else:
            yield a[i]
            i += 1
            j += 1


def run():
    n = int(input())
    a = [int(i) for i in input().split()]
    m = int(input())
    b = [int(j) for j in input().split()]

    for v in intersect(n, m, a, b):
        print(v)


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
n = int(input())
a = set(list(map(int, input().split())))
m = int(input())
b = set(list(map(int, input().split())))
for i in sorted(a&b):
    print(i)