⭐JS | Simple & Explained | Objects => Hashset
705. Design HashSet \uD83D\uDE80\n\n### Solution Developed In:\n\n\n\n## Explaining The Question\n\nIf you understand HashSet\'s you should have no issues solvi
# 705. Design HashSet \uD83D\uDE80\n***\n### Solution Developed In:\n![JavaScript](https://img.shields.io/badge/javascript-%23323330.svg?style=for-the-badge&logo=javascript&logoColor=%23F7DF1E)\n\n\n## Explaining The Question\n\nIf you understand HashSet\'s you should have no issues solving this problem intuitively. The issues with this questions come up when we don\'t know what a ***hashset*** is or how to use one. The other major issue that I\'ve seen is that people are creating inefficient ***hashset*** implementations. Normally creating them already using a ***hashset*** / ***hashmap*** or people are creating solutions that run in linear time and space. \n\nThis is certainly no issue at all. But it isn\'t how they\'re implemented in most programming languages. The way we\'re going to do it is going to be taking advantage of Objects in Javascript. The reason we\'re going to do this is because Objects in Javascript have constant insertion time, constant lookup time, and constant removal time. This is a very efficient data structure. So we\'re going to take advantage of this. \n\nThe created solution pretty much explains itself. \n\n***\n\n##  Recommended Knowledge\n1. [Hashsets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)\n2. [Hashmaps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)\n3. [Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\n4. [Big O Notation](https://en.wikipedia.org/wiki/Big_O_notation)\n5. [Object Oriented Programming](https://en.wikipedia.org/wiki/Object-oriented_programming)\n\n\n## What do we know?\n1. We have a HashSet that we need to create with multiple functions. These function include:\n    1. `add(key)`\n    2. `remove(key)`\n    3. `contains(key)`\n2. We can implement these in mostly ***constant time*** and ***constant space***. \n3. We need to have a good idea of what a Hashset is\n\n## Leetcode Results:\n\nSee Submission Link: \n\n* Runtime:  147 ms faster than ***97.54%*** of JavaScript online submissions for Design HashSet\n* Memory Usage: 51.8 MB, less than ***77.45%*** of JavaScript online submissions for Design HashSet\n\n[![LeetCode](https://img.shields.io/badge/LeetCode-000000?style=for-the-badge&logo=LeetCode&logoColor=#d16c06)](https://leetcode.com/submissions/detail/684786106/)\n\n\n***\n\n# The Solution\n```\nvar MyHashSet = function() {\n  \n    // Really you should just \n    // Make your own object, but instead\n    // we have attached ourself to the \n    // `this` object which then becomes our hashmap.\n    \n    // What you should instead do is this:\n    // this.hash_map = {}\n    // And then update our following functions\n};\n\nMyHashSet.prototype.add = function(key) {\n    \n    // Constant Time\n    // Linear Space | To the size of the input key\n    // You can access objects using array notation\n\n    this[key] = null;\n};\n\nMyHashSet.prototype.remove = function(key) {\n    \n    // Constant Time\n    // Constant Space\n    // You can access objects using array notation\n    // Here we use the delete keyword.\n\n    delete this[key]\n};\n\nMyHashSet.prototype.contains = function(key) {\n    \n    // Constant Time\n    // Constant Space\n    // This just asks if the property exists\n\n    return this.hasOwnProperty(key)\n};\n```
['JavaScript']