JSFiddle - React, Tailwind, and code Playground
by Tim Ko
JavaScript
String => Integer
get ()
insert ()
remove ()
var size = 1000;
var hashmap = Array(size);
{
var key;
var value;
}
function hash(key) {
key % size
}
this.insert = function(key, value) {
var hashIndex = hash(key);
var count = 0;
while (hashmap[hashIndex] != undefined || (hashmap[hashIndex] && hashmap[hashIndex].key != key)) {
hashIndex++ % size;
count++;
if (count > size) {
var newHashmap = Hashmap(size * 2);
for (var i = 0; i < size; i++) {
var oldKey = hashmap[0].key;
newHashmap.insert(key, value);
}
hashmap = newHashmap;
}
}
hashmap[hashIndex] = value;
}
this.get = function(key) {
var hashIndex = hash(key);
var count = 0;
while (count < hashmap.size) {
if (hashmap[hashIndex] && hashmap[hashIndex].key == key) {
return hashmap[hashIndex];
} else if (hashmap[hashIndex] == undefined) {
return -1;
} else {
hashIndex++;
}
}
return -1;
}
this.remove = function(key) {
var hashIndex = hash(key);
delete hashmap[hashIndex];
}