https://programmers.co.kr/learn/courses/30/lessons/42576
function solution(participant, completion) {
var answer = '';
var lengthOfP = participant.length;
participant.sort();
completion.sort();
for(var i=0; i<lengthOfP; i++){
if(participant[i] !== completion [i]){
answer = participant[i];
break;
};
}
return answer;
}
sort를 바로 떠올리지 못함. 있는 그대로 배열끼리 비교하려고 했는데 실패.
힌트참고.
function solution(participant, completion) {
var answer = '';
var obj ={};
var lengthOfParticipant = participant.length;
var lengthOfCompletion = lengthOfParticipant - 1;
for (var i=0; i<lengthOfCompletion; i++){
var personC = completion[i];
if (obj[personC] === undefined){
obj[personC] = 1;
} else {
obj[personC] = obj[personC] + 1;
}
}
for (var j=0; j<lengthOfParticipant; j++){
var personP = participant[j]
if (obj[personP] === 1) {
delete obj[personP];
} else if (obj[personP] === undefined){
answer = personP;
}else{
obj[personP] = obj[personP] - 1;
}
}
return answer;
}