🕊 Link

Lv1. 완주하지 못한 선수 Javascript
https://programmers.co.kr/learn/courses/30/lessons/42576

🧑🏻‍💻 Code(javascript)

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]
    }
  }
}

💡 Solution

  • hash를 이용한 풀이
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;
        }
    }
}

👨🏻‍💻💭 Self Feedback

sort 활용
혹은 map 활용(hash 풀이)


  • 2021.04.16 - 최초 작성
  • 2021.08.09 - hash 풀이 업데이트

댓글 환영 질문 환영
by.protect-me

profile
protect me from what i want

0개의 댓글

관련 채용 정보