Which ports to close?

A port scan shows every open door on a server. Anything open but not needed should be closed.

What to build

Compare the ports a server needs with the ports that are open, and list the ones to close.

Requirements

  • Print the ports to close in increasing order, separated by spaces.
  • If nothing needs closing, print nothing to close.

Input

Line 1: the needed ports. Line 2: the open ports. Both are space-separated lists of numbers.

Output

One line.

Constraints

Each list has 1 to 30 ports, each 1 to 65535, with no repeats in a list.

Example

Input:

22 80 443
21 22 23 80 443 3306

Output:

21 23 3306

Why it works

A port should be closed exactly when it is open and not in the needed list, and sorting the numbers gives a stable order to report.

Examples

Example 1 · Input
22 80 443
21 22 23 80 443 3306
Expected output
21 23 3306
Example 2 · Input
443
443
Expected output
nothing to close

Hints

Hint 1

new Set(neededList) makes membership tests easy with has().

Hint 2

Sort numbers with (a, b) => a - b, not the default sort.

Solution

Show one correct solution (try it yourself first)
const needed = new Set(prompt().trim().split(/\s+/).map(Number));
const open = prompt().trim().split(/\s+/).map(Number);
const close = open.filter((p) => !needed.has(p)).sort((a, b) => a - b);
console.log(close.length ? close.join(" ") : "nothing to close");

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Attack surface and defence in depth →