[Programmers] Hash level1 완주하지 못한 선수
👉정렬👈한 후 반복문으로 비교하는 간단한 방법이 있었다.
function solution(participant, completion) {
participant.sort();
completion.sort();
for (let index = 0; index < participant.length; index++) {
if (participant[index] !== completion[index]) {
return participant[index];
}
}
}
function solution(participant, completion) {
const participantObj = {}; // KEY: 참여자 이름, VALUE: 동명이인 수
for (let name of participant) {
participantObj[name] = participantObj[name] ? participantObj[name] + 1 : 1;
}
// 완주자 목록에 있는 경우 VALUE값 감소
for (let name of completion) {
participantObj[name] -= 1;
}
for (let name in participantObj) {
// completion의 길이는 participant의 길이보다 1 작으므로
if (participantObj[name] === 1) {
return name;
}
}
}