Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImplementQueueUsingStackCompleted_HashSetPending #39

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions HashSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity :add,remove,contains->O(1);
// Space Complexity :O(n); where n is no of elements
// Did this code successfully run on Leetcode :yes
// Any problem you faced while coding this :tried to make array as int array then realised we can use boolean

class MyHashSet {
public int keyList = 1000;// taking keyList Size
public int keyItem = 1001;// taking nested array size as 1001 as item 1000000 will be on //index
// arr[0][1000]
boolean[][] hashSet = new boolean[keyList][];// initializing list

public MyHashSet() {

}

public void add(int key) {
int x = key % keyList;// calculating index using hash function
int y = key / keyList;
if (hashSet[x] == null) {
hashSet[x] = new boolean[keyItem];// initializing nested array
}
hashSet[x][y] = true;// setting value to true

}

public void remove(int key) {
int x = key % keyList;
int y = key / keyList;
if (hashSet[x] != null) {
hashSet[x][y] = false;// removing element by setting it to false at calculated //index
}

}

public boolean contains(int key) {
int x = key % keyList;
int y = key / keyList;
if (hashSet[x] != null) {
return hashSet[x][y];// if nested array is not null return its status
}
return false;// else it will automatically be false

}
}

/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/