Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create Hashing.js #1565

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

Closed
RS-labhub wants to merge 1 commit into TheAlgorithms:master from RS-labhub:patch-2
Closed
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
69 changes: 69 additions & 0 deletions Hashes/Hashing.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<script>
// HashTable Implementation
class HashTable {
constructor() {
this.table = new Array(10);
this.size = 0;
}

// private function to convert key to index
// _ shows that the function is private
_setKey(key) {
return key % 10;
}

// insert value in the hashtable
insert(value) {
const index = this._setKey(value);
this.table[index] = value;
this.size++;
}

get(key) {
const target = this._setKey(key);
return this.table[target];
}

search(value) {
const index = this._setKey(value);
if (this.table[index] == value)
console.log("The value is found at index : ", index);
else
console.log("Not found");
}

delete(key) {
const index = this._setKey(key);

if (this.table[index]) {
this.table[index] = [];
this.size--;
return true;
} else {
return false;
}
}
}

const hashExample = new HashTable();
// insert
hashExample.insert(100);
hashExample.insert(87);
hashExample.insert(86);
hashExample.insert(12);
hashExample.insert(9);


console.log(hashExample.table); // -> shows the hash table

// search
hashExample.search(87); // found
hashExample.search(10); // not found

// delete
hashExample.delete(12);

// showing table after deletion
console.log(hashExample.table);

</script>

AltStyle によって変換されたページ (->オリジナル) /