Data Structures: Perform an Intersection on Two Sets of Data

Could there be any issue with my implementation (I can’t see any and it passes the checks)? As far as I can tell it should work in every case, regardless of whether we create a largeSet or smallSet as in the solution provided, as we are only looking for intersecting values.

Just to be clear, my solution passes. I’m just checking whether there may be any issues with it (code of interest is the intersection() method).


class Set {
constructor() {
  // This will hold the set
  this.dictionary = {};
  this.length = 0;
}
// this method will check for the presence of an element and return true or false
has(element) {
  return this.dictionary[element] !== undefined;
}
// this method will return all the values in the set
values() {
  return Object.keys(this.dictionary);
}
// this method will add an element to the set
add(element) {
  if (!this.has(element)) {
    this.dictionary[element] = true;
    this.length++;
    return true;
  }

  return false;
}
// this method will remove an element from a set
remove(element) {
  if (this.has(element)) {
    delete this.dictionary[element];
    this.length--;
    return true;
  }

  return false;
}
// this method will return the size of the set
size() {
  return this.length;
}
// This is our union method from that lesson
union(set) {
  const newSet = new Set();
  this.values().forEach(value => {
    newSet.add(value);
  })
  set.values().forEach(value => {
    newSet.add(value);
  })

  return newSet;
}
// change code below this line
intersection(otherSet) {
  const newSet = new Set();
  this.values().forEach(value => {
    otherSet.values().forEach(val => {
      if (value === val) {
        newSet.add(value)
      }
    })
  })
  
  return newSet;
}
// change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.

Challenge: Perform an Intersection on Two Sets of Data

Link to the challenge: