05.26에 푼 문제입니다🌷
완주하지 못한 선수
function solution(participant, completion) {
var answer = '';
var com = new Map()
var part = new Map()
participant.map(a=>{
if(part.has(a)) part.set(a,part.get(a)+1)
else part.set(a,1)
})
completion.map(a=>{
if(com.has(a)) com.set(a,com.get(a)+1)
else com.set(a,1)
})
for(var key of part.keys()){
if(com.has(key)){
if(com.get(key)<part.get(key)) return key
}
else return key
}
return answer;
}
효율성 테스트때매 map을 이용해서 푼 문제이다.
중복되는 이름을 가진 선수를 고려해서 참가 map 객체의 key에 이름을 입력하고 value에 중복되는 선수의 수를 입력했다.
completion도 마찬가지이다.
그리고 participant에 있지만 completion에 없는 선수를 return 한다.
중복된 선수는 participant와 completion과 비교해서 participant의 선수 수가 더 많으면 많은 만큼 도착하지 못한 것이므로 해당 선수를 return한다.
function solution(participant, completion) {
var answer = '';
participant=participant.sort()
completion=completion.sort()
for(var i in participant){
if(participant[i] !== completion[i]) return participant[i];
}
return answer;
}
다른 분 풀이
두 배열을 정렬해서 일치하지 않으면 participant에 있고 completion에 없는 선수이므로 return한다.