Skip to content

Commit d333758

Browse files
committed
Hash Table
1 parent 01cf60f commit d333758

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class HashTable {
2+
constructor(size = 6) {
3+
this.keyMap = new Array(size);
4+
}
5+
6+
_hashFunction(key) {
7+
let sum = 0;
8+
const PRIME_NUMBER = 31;
9+
10+
for (let i = 0; i < Math.min(key.length, 100); i++) {
11+
const charCode = key.charCodeAt(0) - 96;
12+
sum = (sum * PRIME_NUMBER + charCode) % this.keyMap.length;
13+
}
14+
15+
return sum;
16+
}
17+
18+
set(key, value) {
19+
const index = this._hashFunction(key);
20+
if (!this.keyMap[index]) this.keyMap[index] = [];
21+
this.keyMap[index].push([key, value]);
22+
return this;
23+
}
24+
25+
get(key) {
26+
const index = this._hashFunction(key);
27+
if (this.keyMap[index]) {
28+
for (let i = 0; i < this.keyMap[index].length; i++) {
29+
if (this.keyMap[index][i][0] === key) {
30+
return this.keyMap[index][i][1];
31+
}
32+
}
33+
}
34+
return undefined;
35+
}
36+
37+
getAllKeys() {
38+
const keys = [];
39+
for (let i = 0; i < this.keyMap.length; i++) {
40+
if (this.keyMap[i]) {
41+
for (let j = 0; j < this.keyMap[i].length; j++) {
42+
keys.push(this.keyMap[i][j][0]);
43+
}
44+
}
45+
}
46+
return keys;
47+
}
48+
49+
getAllValues() {
50+
const values = [];
51+
for (let i = 0; i < this.keyMap.length; i++) {
52+
if (this.keyMap[i]) {
53+
for (let j = 0; j < this.keyMap[i].length; j++) {
54+
values.push(this.keyMap[i][j][1]);
55+
}
56+
}
57+
}
58+
return values;
59+
}
60+
}
61+
62+
const phoneBook = new HashTable();
63+
phoneBook.set("john", "555-333-444");
64+
phoneBook.set("jordan", "222-444-666");
65+
phoneBook.set("michel", "111-666-222");
66+
console.log(phoneBook.get("jordan"));
67+
console.log(phoneBook.getAllValues());
68+
console.log(phoneBook.getAllKeys());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function wordCounter(text) {
2+
const lowerText = text.toLowerCase();
3+
const wordMap = {};
4+
const words = lowerText.split(/\s+/);
5+
6+
for (const word of words) {
7+
if (word in wordMap) {
8+
wordMap[word]++;
9+
} else {
10+
wordMap[word] = 1;
11+
}
12+
}
13+
14+
return wordMap;
15+
}
16+
17+
const text = "Hello my name name name is huxn";
18+
// { hello: 1, my: 1, name: 3, is: 1, huxn: 1 }
19+
const wordCounts = wordCounter(text);
20+
21+
console.log(wordCounts);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function twoSum(nums, target) {
2+
const numMap = {};
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
const complement = target - nums[i];
6+
if (complement in numMap && numMap[complement] !== i) {
7+
return [numMap[complement], i];
8+
}
9+
numMap[nums[i]] = i;
10+
}
11+
12+
return [];
13+
}
14+
15+
const nums = [2, 7, 11, 15];
16+
const target = 9;
17+
const result = twoSum(nums, target);
18+
console.log(result); // [0, 1]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function groupAnagrams(strs) {
2+
const anagramMap = {};
3+
4+
for (const str of strs) {
5+
const sortedStr = str.split("").sort().join("");
6+
7+
if (sortedStr in anagramMap) {
8+
anagramMap[sortedStr].push(str);
9+
} else {
10+
anagramMap[sortedStr] = [str];
11+
}
12+
}
13+
14+
return Object.values(anagramMap);
15+
}
16+
17+
const strs = ["eat", "tea", "tan", "ate", "nat", "bat"];
18+
const groups = groupAnagrams(strs);
19+
20+
console.log(groups);
21+
// Output: [["eat", "tea", "ate"], ["bat"], ["nat", "tan"]]

0 commit comments

Comments
 (0)