Perform a Union on Two Sets*

Tell us what’s happening:
Describe your issue in detail here.
How can I modify this line in order to use it with my older set?

  **Your code so far**

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.values(this.dictionary);
}
// This method will add an element to the set
add(element) {
  if (!this.has(element)) {
    this.dictionary[element] = element;
    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;
}
union(){
  let unionSet= [...new Set([...a, ...b])]//this line
  return unionSet
}
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Perform a Union on Two Sets

Link to the challenge:

So, you want to not create a new set for the union, but mutate the original set to be the union? I think that kind of thing is normally avoided, but if you really want to, could you iterate through the second set and add its members to “this” set?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.