FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript/algorithms/data-structures/hashTable.js at master · AllAlgorithms/javascript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
javascript
Public archive
Notifications
You must be signed in to change notification settings
Fork
43
Star
89
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript
/
algorithms
/
data-structures
/
hashTable.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
76 lines (70 loc) · 1.67 KB
Breadcrumbs
javascript
/
algorithms
/
data-structures
/
hashTable.js
Copy path
File metadata and controls
76 lines (70 loc) · 1.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
*
@author
Rashik Ansar
*
* Implementation of Hash Table
*/
class
HashTable
{
constructor
(
size
=
47
)
{
this
.
keyMap
=
new
Array
(
size
)
;
}
/**
* Hash Function
* Private method to generate hash of the given data
*
@param
{
*
} key data to hash
*/
_hash
(
key
)
{
let
total
=
0
;
const
RANDOM_PRIME
=
31
;
for
(
let
i
=
0
;
i
<
Math
.
min
(
key
.
length
,
100
)
;
i
++
)
{
let
char
=
key
[
i
]
;
let
value
=
char
.
charCodeAt
(
0
)
-
96
;
total
=
(
total
*
RANDOM_PRIME
+
value
)
%
this
.
keyMap
.
length
;
}
return
total
;
}
set
(
key
,
value
)
{
let
index
=
this
.
_hash
(
key
)
;
if
(
!
this
.
keyMap
[
index
]
)
{
this
.
keyMap
[
index
]
=
[
]
;
}
this
.
keyMap
[
index
]
.
push
(
[
key
,
value
]
)
;
return
this
;
}
get
(
key
)
{
let
index
=
this
.
_hash
(
key
)
;
if
(
this
.
keyMap
[
index
]
)
{
for
(
let
i
=
0
;
i
<
this
.
keyMap
[
index
]
.
length
;
i
++
)
{
if
(
this
.
keyMap
[
index
]
[
i
]
[
0
]
===
key
)
{
return
this
.
keyMap
[
index
]
[
i
]
;
}
}
}
return
undefined
;
}
values
(
)
{
let
valArray
=
[
]
;
for
(
let
i
=
0
;
i
<
this
.
keyMap
.
length
;
i
++
)
{
if
(
this
.
keyMap
[
i
]
)
{
for
(
let
j
=
0
;
j
<
this
.
keyMap
[
i
]
.
length
;
j
++
)
{
if
(
!
valArray
.
includes
(
this
.
keyMap
[
i
]
[
j
]
[
1
]
)
)
{
valArray
.
push
(
this
.
keyMap
[
i
]
[
j
]
[
1
]
)
;
}
}
}
}
return
valArray
;
}
keys
(
)
{
let
keysArray
=
[
]
;
for
(
let
i
=
0
;
i
<
this
.
keyMap
.
length
;
i
++
)
{
if
(
this
.
keyMap
[
i
]
)
{
for
(
let
j
=
0
;
j
<
this
.
keyMap
[
i
]
.
length
;
j
++
)
{
if
(
!
keysArray
.
includes
(
this
.
keyMap
[
i
]
[
j
]
[
0
]
)
)
{
keysArray
.
push
(
this
.
keyMap
[
i
]
[
j
]
[
0
]
)
;
}
}
}
}
return
keysArray
;
}
}
Back
|
FazBrowse Home
|
New Git URL