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));
ํด์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํธ๋ ๊ฒ ํจ์จ์ฑ์ด ๋ ์ข๋ค.
'๐กAlgorithm > ๋ฌธ์ ํ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS์๊ณ ๋ฆฌ์ฆ | ์์ ํ์ - ์กธ์ ์ ๋ฌผ (0) | 2021.10.27 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - ์ต์์ง์ฌ๊ฐํ (0) | 2021.10.21 |
JS์๊ณ ๋ฆฌ์ฆ | ์๋๊ทธ๋จ์ด ๋๋ ๋ถ๋ถ๋ฌธ์์ด์ ๊ฐ์ ๊ตฌํ๊ธฐ (0) | 2021.09.24 |
ํ๋ก๊ทธ๋๋จธ์ค(Level 1) - 2์ฃผ์ฐจ_์ํธํ๊ฐ (0) | 2021.09.23 |
ํ๋ก๊ทธ๋๋จธ์ค(Level 1) - ์ซ์๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2021.09.18 |