1. ๋ฌธ์ œ

๋‹จ ํ•œ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ–ˆ๋‹ค. 

์ฐธ์—ฌ ์„ ์ˆ˜์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant

์™„์ฃผํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด completion

์ฐธ๊ฐ€์ž ์ค‘์—๋Š” ๋™๋ช…์ด์ธ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

์ž…๋ ฅ์˜ˆ์ œ | participant ["leo", "kiki", "eden"], completion ["eden", "kiki"]

์ถœ๋ ฅ์˜ˆ์ œ | "leo"

 

 

2. ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ํ’€์—ˆ๋˜ ์ฝ”๋“œ

const solution = (participant, completion) => {
    participant.sort();
    completion.sort();
    for (let i = 0; i < participant.length; i++) {
        if (participant[i] !== completion[i])
            return participant[i];
    }
}

const participant = ["mislav", "stanko", "mislav", "ana"];
const completion = ["stanko", "ana", "mislav"];
console.log(solution(participant, completion));

 

3. ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ‘ผ ์ฝ”๋“œ

const solution = (participant, completion) => {
    let answer = '';

    let map1 = new Map();
    let map2 = new Map();

    for (let x of participant) {
        if (map1.has(x)) { map1.set(x, map1.get(x) + 1) }
        else { map1.set(x, 1) }
    };

    for (let x of completion) {
        if (map2.has(x)) { map2.set(x, map2.get(x) + 1) }
        else { map2.set(x, 1) }
    };

    for (let [key, value] of map1) {
        if (!map2.has(key) || map2.get(key) !== value) {
            answer = key;
        }
    }
    return answer;
}

const participant = ["mislav", "stanko", "mislav", "ana"];
const completion = ["stanko", "ana", "mislav"];
console.log(solution(participant, completion));

ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ‘ธ๋Š” ๊ฒŒ ํšจ์œจ์„ฑ์ด ๋” ์ข‹๋‹ค. 

+ Recent posts