You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hash_tables/hash_tables.md
+5-6Lines changed: 5 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -18,18 +18,20 @@ Internally,a hash table stores its values in an array. A special **hash function
18
18
19
19
Hash function generate a code, known as the **hash code**. Each data type will have its own hash function that generates its hash code differently. It is important to remember that a hash code is not equivalent to the index in the underlying array storage structure--there are often more hash codes than indices in the underlying array.
20
20
21
-
[insert diagram here]
21
+
**Figure below**: Internals of a hash table
22
+

22
23
23
24
24
25
**Note:** When utilizing a hash table with a class you've created, be sure that the hash function for that object type operates as you would expect. If two objects are equivalent, you should ensure that their hash codes are the same.
25
26
26
27
27
28
### Collisions
28
-
If the hash function is implemented well, inputted objects will be distributed evenly across the array indices. However, the number of hash codes is often greater than the size of the underlying array, so some keys will bes assigned the same index. When two keys are matched to the same index, this is called a **collision**. There are several different ways of addressing collisions.
29
+
If the hash function is implemented well, inputted objects will be distributed evenly across the array indices. However, the number of hash codes is often greater than the size of the underlying array, so some keys will be assigned the same index. When two keys are matched to the same index, this is called a **collision**. There are several different ways of addressing collisions.
29
30
30
31
There are multiple approaches to dealing with collisions. The most common one is simply to store all the objects that get assigned to the same index in a linked list. In this scenario, instead of simply storing the value at that index, the linked list must contain both the entire key and the value in pairs instead of just the value, so that the values can be uniquely tied to a key.
31
32
32
-
[insert diagram here]
33
+
**Figure below**: Hash Collision
34
+

33
35
34
36
For more information about other ways to address hash collisions, see the [hash tables Wikipedia page](https://en.wikipedia.org/wiki/Hash_table#Open_addressing) for descriptions of several different approaches. If you have time, I'd encourage you to learn about open addressing, the other common hash collision method.
35
37
@@ -53,6 +55,3 @@ To get a more thorough understanding on the internals of hash tables, here are a
53
55
* Hash table guide with helpful diagrams: https://medium.com/basecs/hashing-out-hash-functions-ea5dd8beb4dd
54
56
* Princeton Coursera video lecture series: https://www.coursera.org/learn/algorithms-part1/lecture/CMLqa/hash-tables
55
57
* HackerRank video (short summary): https://youtu.be/shs0KM3wKv8
0 commit comments