[Algorithm] 중복단어제거 (javaScript)

swing·2023년 6월 21일
0

[Algorithm]

목록 보기
40/96

문제

N개의 문자열이 입력되면 중복된 문자열은 제거하고 출력하는 프로그램을 작성하세요. 출력하는 문자열은 원래의 입력순서를 유지합니다.

입력설명

첫 줄에 자연수 N이 주어진다.(3<=N<=30)
두 번째 줄부터 N개의 문자열이 주어진다. 문자열의 길이는 100을 넘지 않습니다.

출력설명

첫 줄부터 중복이 제거된 문자열을 차례로 출력한다.

입출력예제

입력
5
good
time
good
time
student

출력
good
time
student

문제 해결

const solution = (input) => {
  const [N, ...words] = input.split("\n");
  return [...new Set(words)].join("\n");
};

const a = solution("5\ngood\ntime\ngood\ntime\nstudent");

console.log(a); // good \n time \n student

// 다른 방법
const solution = (input) => {
  const [N, ...words] = input.split("\n");
  return words.filter((v,i)=>words.indexOf(v) === i).join('\n');
};

const a = solution("5\ngood\ntime\ngood\ntime\nstudent");

console.log(a); // good \n time \n student
profile
if(기록📝) 성장🌱

0개의 댓글