Accessing collection of a Set

Accessing collection of a Set
0.0 0

#1

Hi all, I’m currently working on writing a .union() method for a Set class, under the new ‘Data Structure’ section. The prompt is here: https://learn.freecodecamp.org/coding-interview-prep/data-structures/perform-a-union-on-two-sets

My code is as follows:

function Set() {
    // the var collection will hold the set
    var collection = [];
    // this method will check for the presence of an element and return true or false
    this.has = function(element) {
        return (collection.indexOf(element) !== -1);
    };
    // this method will return all the values in the set
    this.values = function() {
        return collection;
    };
    // this method will add an element to the set
    this.add = function(element) {
        if(!this.has(element)){
            collection.push(element);
            return true;
        }
        return false;
    };
   // this method will remove an element from a set
    this.remove = function(element) {
        if(this.has(element)){
           var index = collection.indexOf(element);
            collection.splice(index,1);
            return true;
        }
        return false;
    };
    // this method will return the size of the set
    this.size = function() {
        return collection.length;
    };
    // change code below this line
    this.union = function(set){
        let countObj = {};
        let tmpColl = [];

        collection = [...collection, ...set.collection];

        collection.forEach(arrElem => {
            if(countObj[arrElem]){
                
            }
            else{
                countObj[arrElem] = true;
                tmpColl.push(arrElem);
            }
        });

        collection = tmpColl;
        console.log(collection);
        return collection;
    }
    // change code above this line
}

However, I get an error “Cannot convert undefined or null to object”. I’m relatively sure this is caused by being unable to access the Set.collection, since in the original prompt the collection was set with “var collection = []”.
I know I can get around this problem by writing:

this.collection = [];
var collection = this.collection;

and independently testing it, my code works. However, if I change it thusly, the code will not pass the FCC test (we’re also instructed specifically to not change that part of the code). Are there any other ways to access the Set’s collection?

EDIT: So I realized that they’d included a .values() method to return collection. I then tried:

 this.union = function(set){
        let countObj = {};
        let tmpColl = [];

        collection = [...collection, ...set.values()];

        collection.forEach(arrElem => {
            if(countObj[arrElem]){
                
            }
            else{
                countObj[arrElem] = true;
                tmpColl.push(arrElem);
            }
        });

        collection = tmpColl;
        console.log(collection);
        return collection;
    }

but got the error: ‘final.indexOf is not a function’. I’m lost as to what this error means, as I did not run into when I tested it outside of FCC. Still have no idea how to fix this.


#2

Your second approach is right, beside you use if-else like if-ifn't
this could be better with this form?

collection.forEach(arrElem => {
            if(!countObj[arrElem]){
                 countObj[arrElem] = true;
                tmpColl.push(arrElem);
            }
        });

The issue is you return collection(array), while union supposed to return a Set.

FIX: Just replace return collection to return this will work.

I’m not js expert, and I’m not sure if js return this as reference or a copy, but if it’s as a reference, you should consider returning this, is not the same when you create one new instance.

Logically in OOP(not in js probably) union could be a class level, or instance level function, they are different. but for this assignment it passes either you union current/this object/collection, or return a new Set instance.

In JS also(js dev) not taking performance stuff so serious, as you could run and code the union in much lighter form.


#3

You don’t have to return collection . You need to return this