๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/์ธํ”„๋Ÿฐ(inflearn)

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] 16 - ์ค‘๋ณต ๋‹จ์–ด ์ œ๊ฑฐ

by YWTechIT 2021. 8. 12.
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"));
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€