From 3f3cf286670f924afde1a641ae4480c835a5fbdd Mon Sep 17 00:00:00 2001 From: Manish Date: Tue, 22 Aug 2023 10:59:12 +1000 Subject: [PATCH] Data Structures: Create a Set Class --- Data Structures/setClass.js | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Data Structures/setClass.js diff --git a/Data Structures/setClass.js b/Data Structures/setClass.js new file mode 100644 index 0000000..0758d4c --- /dev/null +++ b/Data Structures/setClass.js @@ -0,0 +1,41 @@ +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 +}