freeCodeCamp/Data Structures/setClass.js

42 lines
850 B
JavaScript
Raw Permalink Normal View History

2023-08-22 02:59:12 +02:00
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;
}
// This method will return all the values in the set
values() {
return Object.keys(this.dictionary);
}
// Only change code below this line
add(item) {
if (this.dictionary[item] === undefined) {
this.dictionary[item] = true;
this.length++;
return true;
}
return false;
}
remove(item) {
if (this.dictionary[item] === undefined) {
return false;
}
delete this.dictionary[item];
this.length--;
return true;
}
size() {
return this.length;
}
// Only change code above this line
}