This repository provides a practical example of implementing a HashMap (also known as a Dictionary or Hashtable) data structure in Ruby. While Ruby’s Hash class offers built-in hash mapping functionality, this project serves as an educational resource for students and developers involved in computer science (CS), computer engineering (CE), and related fields.
HashMaps are data structures that store key-value pairs, allowing for efficient data retrieval. Rather than storing values in a sequence, they use a hash function to map each key to an index in an underlying array or bucket. This enables faster lookups compared to sequential search data structures like arrays.
In a HashMap, memory allocation depends on the hash function used. Each key-value pair is stored in a bucket or slot, with collisions being handled through techniques such as chaining or open addressing. This approach offers dynamic memory allocation for each key-value pair, unlike arrays where memory is allocated contiguously.
The average time complexity for basic operations in a HashMap is O(1), including inserting, deleting, and searching for elements, thanks to the hash function directing operations to the appropriate index. However, in the case of hash collisions, the time complexity can increase to O(n) in the worst case (though typically, it remains near O(1) with good hashing techniques and proper resizing).