Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}

21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Table of Contents
[1- code challange class 2 array shift ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/3)
# Hashtables

## Challenge
Implement a Hashtable with the following methods:

- add: takes in both the key and value. This method should hash the key, and add the key and value pair to the table, handling collisions as needed.
- get: takes in the key and returns the value from the table.
- contains: takes in the key and returns a boolean, indicating if the key exists in the table already.
- hash: takes in an arbitrary key and returns an index in the collection.

## Approach & Efficiency
- add Time - O(1)
- get Time - O(1)
- contains
Time - O(1)
Space - O(1)
## API

[2- code challange class 3 array binary search ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/2)

[
58 changes: 58 additions & 0 deletions challanges/hashTable/hashTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';
class Hashmap {
constructor(size){
this.arr = new Array(size);
this.size = size;
}

hash(key){
return key.split('').reduce((acc, val) => {
return acc + val.charCodeAt(0);
}, 0) * 19 % this.size;
}

add(key, val){
let index = this.hash(key);

if(!this.arr[index]){
this.arr[index] = [];
}

this.arr[index].push([key,val]);
return index;
}

get(key){
const hashed = this.hash(key);
const array = this.arr[hashed];
if(array === null || array === undefined){
return null;
}

for(let i = 0; i < array.length; i++){
if(array[i][0] === key){
return array[i][1];
}
}
return null;
}

contains(key){
const hashed = this.hash(key);
const array = this.arr[hashed];
if(array === null || array === undefined){
return false;
}

for(let i = 0; i < array.length; i++){
if(array[i][0] === key){
return true;
}

}
return false;
}
}

module.exports = Hashmap;

19 changes: 19 additions & 0 deletions challanges/hashTable/hashTable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict ';

const Hashmap = require('./hashTable.js');

describe('Hashmap', () => {
it('add ', () => {
const hashmap = new Hashmap(1024);
hashmap.add('name', 'angel');
expect(hashmap.contains('name')).toBe(true);
});


it('Successfully handle a collision within the hashtable', () => {
const hashmap = new Hashmap(1024);
hashmap.add('name', 'angel');
hashmap.add('class', '401');
expect(hashmap.get('class')).toBe('401');
});
});
Loading