출처 : 프로그래머스
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.
마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.
| participant | completion | return |
|---|---|---|
| [leo, kiki, eden] | [eden, kiki] | leo |
| [marina, josipa, nikola, vinko, filipa] | [josipa, filipa, marina, nikola] | vinko |
| [mislav, stanko, mislav, ana] | [stanko, ana, mislav] | mislav |
예제 #1 leo는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #2 vinko는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #3 mislav는 참여자 명단에는 두 명이 있지만, 완주자 명단에는 한 명밖에 없기 때문에 한명은 완주하지 못했습니다.
function solution(participant, completion) {
let hash = {}
for(const a of participant){
if(hash.hasOwnProperty(a)) hash[a] += 1
else hash[a] = 1
}
for(const b of completion){
hash[b] -= 1
}
return participant.filter(ele => hash[ele] !== 0)[0]
}function solution(participant, completion) {
participant.sort()
completion.sort()
for(let i=0; i<participant.length; i++){
if(participant[i] !== completion[i]) return participant[i]
}
}