take on averageO(1) time for insertions and lookups
are unordered (the keys are not guaranteed to stay in the same order)
can use many types of objects as keys (commonly strings)
Hash tables can be thought of as arrays, if you think of array indices as keys!
In fact, hash tables are built on arrays. So if you ever want to use a hash table but know your keys will be sequential integers (like 1..100), you can probably save time and space by just using an array instead.
Note: hash tables have an average case insertion and lookup cost of O(1). In industry, we often confuse the average-case cost with worst case cost, but they're not really the same. Because of hash collisions and rebalancing, a hash table insertion or lookup can cost as much as O(n) time in the worst case. But usually in industry we assume hashing and resizing algorithms are clever enough that collisions are rare and cheap.
, where the keys are words and the values are the number of times the words occurred.
Think about capitalized words. For example, look at these sentences:
'After beating the eggs, Dana read the next step:' 'Add milk and eggs, then add flour and sugar.'
What do we want to do with "After", "Dana", and "add"? In this example, your final dictionary should include one "Add" or "add" with a value of 2. Make reasonable (not necessarily perfect) decisions about cases like "After" and "Dana".
No comments:
Post a Comment