Sets

Updated on by ,

Set
An unordered collection of objects.

Roster Notation

The easiest way to express a set is to use roster notation — this basically lists all of the unique objects belonging to the set. It looks like this:

A={1,2,3,4,5}A=\{1,2,3,4,5\}

Pretty simple, right?

Set Membership

An object that is in a set is considered a member of that set. Membership is denoted by the \in symbol. If we take set AA above as an example, we could say 4A4 \in A, or 44 is a member of AA. Conversely, we could say that 6A6 \notin A, or 66 is not a member of AA.

Special Sets

The empty set: ϕ\phi

N\mathbb{N} - The Natural Numbers, {0,1,2,3,4,...}\{0,1,2,3,4,...\} or {1,2,3,4,...}\{1,2,3,4,...\}

Z\mathbb{Z} - Integers, {...,3,2,1,0,1,2,3,...}\{...,-3,-2,-1,0,1,2,3,...\}

Q\mathbb{Q} - The rational numbers

R\mathbb{R} - The real numbers

UU - Universe set, the set of all elements

P()P() - The power set of any set

Set Relationships

Set Operations

JavaScript Set Operations

Sets in JavaScript are data structures that replicate, more or less, some of the basic behavior of mathematical sets. They are collections of unique elements.

I’m not going to go over everything you can do with JS sets, just a few set operations. If you wantt to read more head over to MDN.

Let’s set up some simple exercises so we can play around with sets.

addAll Function

It can feel like a real pain to add individual items to js sets. Here’s a function that accepts a set and any number of objects and adds all the objects to the set.

const addAll = function (set, ...members) {
  members.forEach((member) => set.add(member));
  return set;
};

After creating the above function, it’s pretty easy to instantiate new sets. I’m going to go ahead and create a few and then use them for some exercises.

const U = new Set(); // the universe for this exercise

for (let i = 0; i < 50; i++) {
  U.add(i);
}

const A = addAll(new Set(), 1,2,3,4,5);
const B = addAll(new Set(), 6,7,8,9,10);
const C = addAll(new Set(),1,3,5,7,9,11);
const D = addAll(new Set(),2,4,6,8,10);
const E = addAll(new Set(),12,14,16,18);

With the above code we have:

U={x:xZ,50>x>0}U=\{x : x \in \mathbb{Z}, 50 > x > 0 \} - the universe, or the domain for all sets to follow.

A={1,2,3,4,5}A=\{1,2,3,4,5\}

B={6,7,8,9,10}B=\{6,7,8,9,10\}

C={1,3,5,7,9,11}C=\{1,3,5,7,9,11\}

D={2,4,6,8,10}D=\{2,4,6,8,10\}

E={12,14,16,18}E=\{12,14,16,18\}

const isSubset = function(lhs, rhs) {
  for (let member of lhs) {
    if (!rhs.has(member)) {
      return false;
    }
  }

  return true;
};

const isSuperSet = function(lhs,rhs) {
  for (let member of rhs) {
    if (!lhs.has(member)) {
      return false;
    }
  }

  return true;
}

const cardinality = function(set) {
  return set.size;
}

const union = function(...sets) {
  const setUnion = new Set();

  sets.forEach((set) => {
    set.forEach((member) => setUnion.add(member));
  })

  return setUnion;
};

const intersection = function (...sets) {
  const setIntersection = new Set();

  sets.reduce((previous, current) => {
    current.forEach((member) => {
      if(previous.has(member)) {
        setIntersection.add(member);
      }
    });
  });

  return setIntersection;
};

WebMentions

No mentions yet

Say something about this post!