Middle
What is a hash table?
sobes.tech AI
Answer from AI
A hash table is a data structure that provides fast access to elements by key. It uses a hash function that converts the key into an array index where the value is stored.
Main characteristics:
- On average, insertion, search, and deletion operations are performed in O(1).
- To handle collisions (when different keys produce the same index), methods such as chaining (linked lists) or open addressing are used.
Example in Go (using the built-in map):
m := make(map[string]int)
m["apple"] = 5
value, ok := m["apple"]
if ok {
fmt.Println("Value:", value)
}
Here, map is a hash table where the key is a string and the value is an integer.