[백준1822_자바스크립트(javascript)] - 차집합

경이·2024년 11월 23일

𝑩𝑶𝑱 (𝒋𝒔)

목록 보기
270/325

🔴 문제

차집합


🟡 Sol

const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : 'input.txt';
const [_, A, B] = fs
  .readFileSync(path)
  .toString()
  .trim()
  .split('\n')
  .map((it) => it.split(' ').map(Number));

const set = new Set();

for (const a of A) {
  set.add(a);
}

for (const b of B) {
  if (set.has(b)) set.delete(b);
}

console.log(set.size);
console.log(...Array.from(set).sort((a, b) => a - b));

🟢 풀이

⏰ 소요한 시간 : -

집합 AB의 크기가 최대 500,000이기에 set 객체로 풀이했다.
집합 A에서 B와 겹치는 부분을 제거해주면 된다.
따라서 set객체에 A의 모든 원소를 넣고, B의 모든 원소를 순회하면서 만약 set 객체에 존재하는 경우 제거해주면 된다.


🔵 Ref

profile
록타르오가르

0개의 댓글