Lv1. 완주하지 못한 선수 Javascript
https://programmers.co.kr/learn/courses/30/lessons/42576
function solution(par, com) {
par.sort();
com.sort();
for(let i=0; i < par.length + 1; i++) {
if (par[i] !== com[i]) {
return par[i]
}
}
}
function solution(participant, completion) {
const myMap = new Map();
// 같은 key를 가진 값이 있을 경우 ++, 없을 경우 1을 세팅
for ( const participant of participants){
if(!myMap.get(participant)){
myMap.set(participant, 1);
}else{
myMap.set(participant, myMap.get(participant)+1);
}
}
// 같은 key를 가진 값이 있을 경우 --
for(const completion of completions){
if(myMap.get(completion)){
myMap.set(completion, myMap.get(completion)-1);
}
}
// 0이 아닌 값을 찾음
for(const participant of participants){
if(myMap.get(participant) && myMap.get(participant) >=1 ){
answer = participant;
}
}
}
sort 활용
혹은 map 활용(hash 풀이)
댓글 환영
질문 환영
by.protect-me