์ค๋ณต ๋จ์ด ์ ๊ฑฐ(๊ธฐ๋ณธ ๋ฌธ์ ํ์ด)
๋ฌธ์ (์ถ์ฒ : ์ธํ๋ฐ ์๋ฐ์คํฌ๋ฆฝํธ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด ๊ฐ์, ์ ๋ณด์ฌ๋ฆผํผ์๋)
N๊ฐ์ ๋ฌธ์์ด์ด ์ ๋ ฅ๋๋ฉด ์ค๋ณต๋ ๋ฌธ์์ด์ ์ ๊ฑฐํ๊ณ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์ธ์. ์ถ๋ ฅํ๋ ๋ฌธ์์ด์ ์๋์ ์ ๋ ฅ์์๋ฅผ ์ ์งํฉ๋๋ค.
โฃ ์
๋ ฅ์ค๋ช
์ฒซ ์ค์ ์์ฐ์ N์ด ์ฃผ์ด์ง๋ค.(3<=N<=30)
๋ ๋ฒ์งธ ์ค๋ถํฐ N๊ฐ์ ๋ฌธ์์ด์ด ์ฃผ์ด์ง๋ค. ๋ฌธ์์ด์ ๊ธธ์ด๋ 100์ ๋์ง ์์ต๋๋ค.
โฃ ์ถ๋ ฅ์ค๋ช
์ฒซ ์ค๋ถํฐ ์ค๋ณต์ด ์ ๊ฑฐ๋ ๋ฌธ์์ด์ ์ฐจ๋ก๋ก ์ถ๋ ฅํ๋ค.
โฃ ์ ๋ ฅ์์ 1
5
good
time
good
time
student
โฃ ์ถ๋ ฅ์์ 1
good
time
student
Tip
1) ๋น ๋ฐฐ์ด์ ๋ด์ ๋ณ์๋ฅผ ํ๋ ๋ง๋ ๋ค.(let result = [];)
2) ์ธ์๋ก ๋ฐ์ ๋ฐฐ์ด์ ๋ฐ๋ณต๋ฌธ์ ๋๋ฆฐ๋ค.
3) indexOf ๋ฉ์๋ / includes ๋ฉ์๋๋ฅผ ํ์ฉํ์ฌ result ๋ณ์์ ๋ฐ๋ณต๋๋ ์์๊ฐ ์๋ค๋ฉด ์ถ๊ฐํด ์ค๋ค.
๋ฌธ์ ํ์ด
//๋ด๊ฐ ์์ฑํ ๋ต(includes ํ์ฉ) <-- ์ด๊ฒ ๋ ๋ด ๋์๋ ์ง๊ด์ ์ธ๊ฑฐ ๊ฐ์
function solution(...words){
result = [];
for(let x of words){
if(!result.includes(x)) result.push(x);
}
return result;
}
console.log(solution('good','time','good','time','student'));
//๋ด๊ฐ ์์ฑํ ๋ต(indexOf ํ์ฉ)
function solution(...words){
result = [];
for(let i = 0 ; i < words.length ; i++){
if(words.indexOf(words[i]) === i) result.push(words[i]);
}
return result;
}
console.log(solution('good','time','good','time','student'));