From e0c1357fe8aed2611c026c18c60ddce7bb8460e2 Mon Sep 17 00:00:00 2001 From: Manish Date: Thu, 24 Aug 2023 11:58:24 +1000 Subject: [PATCH] Data Structures: Create a Map Data Structure --- Data Structures/map.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Data Structures/map.js diff --git a/Data Structures/map.js b/Data Structures/map.js new file mode 100644 index 0000000..c1ebcbd --- /dev/null +++ b/Data Structures/map.js @@ -0,0 +1,34 @@ +// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-map-data-structure + +var Map = function () { + this.collection = {}; + // Only change code below this line + this.add = (k, v) => { + this.collection[k] = v; + }; + + this.remove = (k) => { + delete this.collection[k]; + }; + + this.get = (k) => { + return this.collection[k]; + }; + + this.has = (k) => { + return this.collection[k] !== undefined; + }; + + this.values = () => { + return Object.values(this.collection); + }; + + this.size = () => { + return Object.keys(this.collection).length; + }; + + this.clear = () => { + this.collection = {}; + }; + // Only change code above this line +};