Hash vs. Array: which is faster for lookups and why?
Last updated
Hashlookup by key is O(1) on average because the key is hashed straight to a bucket.Searching an
Arrayby value (include?,find) is O(n). Only access by known index likearr[5]is O(1).Use a Hash or
Setfor keyed or membership lookups, and an Array for ordered, index-addressed data.