The answers so far have helped identify hash tables and explain some theories, but I think an example can help you better understand them.
What is the difference between a hash table and a regular array?
A hash table and an array are structures that allow you to store and retrieve data. Both allow you to specify the index and get the value associated with it. The difference, as Daniel Spivak noted, is that array indexes are sequential and hash table tables are based on the value of the data associated with them.
Why should I use a hash table?
A hash table can provide a very efficient way to search for items in large amounts of data, especially data that otherwise might not be easily searchable. (“Big” here means ginormous in the sense that it will take a long time to search sequentially).
If I were to code a hash, how could I start?
No problems. The easiest way is to invent an arbitrary mathematical operation that you can perform on data that returns the number N (usually an integer). Then use this number as an index in the bucket array and save your data in bucket # N The trick is to choose an operation that tends to place values in different buckets so that it is easier to find them later.
Example: A large shopping center stores a database of cars and parking lots for its visitors to help customers remember where they are parked. The database stores make , color , license plate and parking location . After leaving the store, the buyer finds his car by entering its brand and color. The database returns a (relatively short) list of license plates and parking spaces. A quick scan allows you to find a buyer.
You can implement this with an SQL query:
SELECT license, location FROM cars WHERE make="$(make)" AND color="$(color)"
If the data was stored in an array, which is essentially just a list, you can imagine implementing a query by scanning the array for all the relevant records.
On the other hand, imagine a hash rule:
Add the ASCII character codes of all letters in make and color, divide them by 100, and use the remainder as the hash value.
This rule converts each item into a number from 0 to 99, essentially sorting the data into 100 buckets. Each time a client needs to find a car, you can make a hash and color to find one bucket of 100 that contains information. You immediately reduced your search by 100 times!
Now expand the example to huge amounts of data, for example, a database with millions of records, which are searched according to dozens of criteria. A “good” hash function will distribute the data in buckets in such a way as to minimize any additional searches, saving a significant amount of time.