TypeScript bootcamp · Lab 20

Set intersection

mediumSets12 minLesson: Set

Read the question, write TypeScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read two lines of integers. Print the sorted intersection, space-separated.

Input. Two lines of integers.

Output. Common values sorted.

Examples

Example 1
Input
1 2 3 4
3 4 5
Output
3 4
Hint
  1. Use Set; filter a by has; sort.
Show correct code

Peek only after you have tried. You can still Check your own version.

const a = readLine().trim().split(/\s+/).map(Number);
const b = new Set(readLine().trim().split(/\s+/).map(Number));
console.log(a.filter((x) => b.has(x)).sort((x, y) => x - y).join(' '));
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.