• 방법 1. 시간초과.. 딕셔너리 이용
import sys
input = sys.stdin.readline
n = int(input())
sg = list(map(str, input().split()))
sg2 = set(sg) # 중복제거
cnt = 0
dic = {}
for i in sg2:
cnt = sg.count(i) # 상근이 카드 번호 마다 몇장이 있는지
dic[i] = cnt # 딕셔너리에 저장(추가)
m = int(input())
nums = list(map(str, input().split()))
for j in nums: # 입력받은 수가
if j in sg2: # 상근이 카드에 있으면
print(dic[j], end=" ") # 딕셔너리에서 value값 출력
else:
print(0, end=" ") # 없으면 0 출력
• 방법 2. 정답!! Counter 이용
import sys
from collections import Counter # ** Counter 사용시 import 해줘야 함
input = sys.stdin.readline
n = input().rstrip()
card = list(map(int, input().split())) # 상근이 카드
m = input().rstrip()
test = list(map(int, input().split())) # 비교하고싶은 숫자
cnt = Counter(card) # Counter({10:3, 3:2, -10:2, 7:1, 6:1, 2:1})
for i in test: # 비교하고 싶은 숫자가
if i in cnt: # cnt에 있으면
print(cnt[i], end=" ")
else:
print(0, end=" ")
• 처음에 방법1로 했을때 테스트케이스 실행은 되었지만 시간초과가 떴다.
• 아래 블로그를 참고해 Counter를 사용하니 해결되었다.