
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));
⏰ 소요한 시간 : -
집합 A와 B의 크기가 최대 500,000이기에 set 객체로 풀이했다.
집합 A에서 B와 겹치는 부분을 제거해주면 된다.
따라서 set객체에 A의 모든 원소를 넣고, B의 모든 원소를 순회하면서 만약 set 객체에 존재하는 경우 제거해주면 된다.