JSFiddle - React, Tailwind, and code Playground

by ysuper

HTML

<h2>
<a href="https://blog.techbridge.cc/2017/02/11/javascript-data-structure-algorithm-set/" target="_blank">用 JavaScript 學習資料結構和演算法:集合(Set)篇</a>
</h2>

JavaScript

function eqSet(as, bs) {
  if (as.size !== bs.size) return false;
  for (var a of as)
    if (!bs.has(a)) return false;
  return true;
}

function inSet(as, bs) {
  for (var a of as)
    if (bs.has(a)) return true;
  return false;
}

function union(firstSet, otherSet) {
  return new Set([...firstSet, ...otherSet])
}


a = new Set([1, 3, 5])
b = new Set([1,4,6])
/* b = new Set() */
console.log(eqSet(a, b))
console.log(inSet(a, b))
console.log(union(a, b))