Create a set class in interview prep

can anyone tell me why this exercise is using an object, with keys same as values?
it seems redundant. why not a simple array?

  **Your code so far**
class Set {
constructor() {
  // Dictionary will hold the items of our 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;
}
print(){
console.log(this.dictionary)
}
// This method will return all the values in the set
values() {
  return Object.values(this.dictionary);
}

// Only change code below this line
// This method will add element to the set
add(element) {
  if (!this.has(element)) {
    this.dictionary[element] = element;
    this.length++;
    return true;
  } else {
    return false;
  }
}

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

// This will return the length of the set
size() {
  return this.length;
}
// Only change code above this line
}
let dictoset = new Set 
dictoset.add('banana')
dictoset.print()
dictoset.add('appz')
dictoset.print()

console.log(dictoset.has('banana'))
console.log(dictoset.length)
console.log('ooooo',dictoset.remove('banana'))
dictoset.print()
console.log('pppppppp',dictoset.remove('tgb'))
dictoset.print()
  **Your browser information:**

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

Challenge: Create a Set Class

Link to the challenge:

Do you understand how a set works? I would make sure you have a good understanding of that first.

2 Likes

Typically the insertion order for a set does not matter. If it does, you could still use an object but the data structure would probably function more like a linked list. Also, if order matters, you could still create an array to keep the insertion order but you would still need an object to keep track of an element’s index in the array in order to keep the time it takes to remove the item O(1).

This particular challenge assumes insertion order does not matter which is why an object is fine.

1 Like

so we are creating our own Set constructor here, whilst it is otherwise possible to use a standard js one?
and we are expected to have key=value in our dictionary?

(i have just come across something that seems to give values only in an object:

let chars = new Set(['a', 'a', 'b', 'c', 'c']);
console.log(chars);

i’ll figure that out i suppose, if we don’t need to do something like that in this exercise)

Under the hood, a JS object is a hash map, so by using a dictionary object, you get O(1) lookups instead of O(n) you would get by searching and modifying an arry every time you want to add or remove an item.

I’ve got plenty to look up from this chat, but are you saying the key-value pair is more efficient in this implementation?

I’m saying that using an object for the internal ‘dictionary’ is more efficient than using an array, yes. It’s also more consistent with the typical way a Set is created.

1 Like

It’s also kinda how the Set data structure is implemented in modern JS engines. There is some quite a lot of complexity in top of the basic implementation you’re doing (so as to optimise things, but note that most of the code is actually written in JS so it’s not too far off what you’re doing here).

2 Likes