Skip to content

Instantly share code, notes, and snippets.

@black1277
Created March 5, 2022 22:06
Show Gist options
  • Save black1277/118f74040fe233546562852cfaef07f8 to your computer and use it in GitHub Desktop.
Save black1277/118f74040fe233546562852cfaef07f8 to your computer and use it in GitHub Desktop.
Операции над множествами
Объединение без дублирования:
const a = new Set([1,2,3]);
const b = new Set([4,3,2]);
const union = new Set([...a, ...b]);
// {1,2,3,4}
Пересечение:
const a = new Set([1,2,3]);
const b = new Set([4,3,2]);
const intersection = new Set(
[...a].filter(x => b.has(x))
);
// {2,3}
Разность:
const a = new Set([1,2,3]);
const b = new Set([4,3,2]);
const difference = new Set(
[...a].filter(x => !b.has(x)));
// {1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment