Junior — Middle
Can you explain what a hash table data structure is and how it works?
sobes.tech AI
Answer from AI
A hash table is a data structure that stores key-value pairs and provides very fast access to data by key. It works as follows: the key is passed through a hash function, which transforms it into an array index where the value is stored.
Main points:
- Fast access: search, insert, and delete operations are usually performed in near O(1) time.
- Collisions: different keys can have the same index; methods like chaining (linked lists) or open addressing are used to handle them.
Example in Dart (Flutter):
void main() {
var map = <String, int>{};
map['apple'] = 3;
map['banana'] = 5;
print(map['apple']); // 3
}
Here, map is a hash table where the key is a string and the value is a number.