Sobes.tech
Junior — Middle

Can you give examples of where hash table data structure is used?

sobes.tech AI

Answer from AI

Hash tables are widely used for fast data access by key. Examples of use:

  • Implementation of dictionaries and associative arrays (e.g., dict in Python).
  • Caching data to speed up repeated requests.
  • Indexing databases for quick search.
  • Counting the frequency of element occurrence (e.g., words in a text).
  • Implementing sets (set) for fast search and deletion of elements.

Example in Python:

# Counting the number of words in a list
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
count = {}
for word in words:
    count[word] = count.get(word, 0) + 1
print(count)  # {'apple': 3, 'banana': 2, 'orange': 1}