DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ (JavaScript) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ (JavaScript)

gimewn 2022. 8. 26. 00:14

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/42576?language=javascript 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ“Œ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ ํ†ต๊ณผ ๋ชปํ•œ ์ฝ”๋“œ

function solution(participant, completion) {
    var answer = "";
    participant.map((word)=>{
        if(completion.includes(word)){
            let where = completion.indexOf(word)
            completion.splice(where, 1)
        }else{
            answer = word;
        }
    });
    
    return answer;
}

 

๐Ÿ“Œ ํšจ์œจ์„ฑ๊นŒ์ง€ ํ†ต๊ณผํ•œ ์ฝ”๋“œ

function solution(participant, completion) {
    var answer = "";
    let isComple = {};
    participant.map((word)=>{
        if(word in isComple){
            isComple[word] += 1
        }else{
            isComple[word] = 1
        }
    })
    completion.map((word)=>{
        isComple[word] -= 1
    })
    let keys = Object.keys(isComple)
    keys.map((word)=>{
        if(isComple[word] === 1){
            answer = word;
        }
    })
    return answer;
}