수많은 마라톤 선수들이 마라톤에 참여하였습니다.
단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.
마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.
프로그래머스 코딩테스트 연습에 있는 해시에 대한 문제들 중 완주하지 못한 선수 문제이다.
해싱은 데이터를 아주 빠르게 삽입하거나 가져올 때 사용하는 기법이라고 한다.
이에따라 파이썬 자료구인 dictionary를 사용하였으며 구현한 코드는 다음과 같다.
사용한 자료구조 : Dictionary
def solution(participant, completion):
result = dict()
for i in range(len(participant)):
if(participant[i] in result):
result[participant[i]] += 1
else:
result[participant[i]] = 1
for i in range(len(completion)) :
result[completion[i]] -= 1
for k, v in result.items() :
if v != 0:
return(k)
break
문제출처 : 프로그래머스 https://programmers.co.kr/