<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
<script>
let a = new Set([1, 2, 3, 4, 5]);
let b = new Set([4, 5, 2, 9]);
// 并集
console.log(new Set([...a, ...b]));
// 差集
console.log(
new Set(
[...a].filter(function (item) {
return !b.has(item);
})
)
);
// 交集
console.log(
new Set(
[...a].filter(function (item) {
return b.has(item);
})
)
);
</script>
</html>
js 差集算法实现
最新推荐文章于 2024-06-14 17:08:32 发布