728x90
๐ 16 - ์ค๋ณต ๋จ์ด ์ ๊ฑฐ
์ค๋ณต ๋ฌธ์ ์ ๊ฑฐ์ ๊ฐ์ ๋ก์ง์ผ๋ก ํ์๋๋ฐ ์ด๋ฒ์ ๋ฐฐ์ด ์์ ๋ฌธ์๊ฐ ๋ค์ด๊ฐ์๋ ๋ฌธ์ ๋ค. set
, indexOf
, indexOf + filter
๋ฅผ ์ฌ์ฉํ๋ค.
solution(5, ["good", "time", "good", "time", "student"]);
// set
function solution(s) {
return [...new Set(s)].join("\n");
}
// indexOf
function solution(n, words) {
for (let i = 0; i < n; i++){
if(words.indexOf(words[i]) === i){
console.log(words[i])
}
}
}
// filter + indexOf
function solution(n, words) {
let ans = words.filter((item, idx) => words.indexOf(item) === idx);
console.log(ans.join("\n"));
}
๋ฐ์ํ
๋๊ธ