Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 6
    As well as the above, if you are using immutable objects as your keys e.g. Java Strings, having calculated the hash once, you can remember it and not have to calculate it again. On the other hand, you can't usually rely on the hash to tell if two keys are equal once you have found the right bucket, so for strings you need to do an O(m) traversal to find out if they are equal. Commented May 5, 2010 at 8:05
  • 1
    @JeremyP: Good point on the O(m) equality comparison. I missed that - updated post. Thanks! Commented May 5, 2010 at 8:22
  • 3
    The O(1) claim is true if you're hashing ints or something else that fits in a machine word. That's what most theory on hashing assumes. Commented May 30, 2014 at 12:40
  • I like that explanation of yours Mark, I quoted it on my article about hash tables on meshfields.de/hash-tables Commented Jun 2, 2014 at 23:43
  • 3
    In "m is the length of the input" - input is overly vague - it might mean all keys & values being inserted, but it becomes clear later (at least to those who already understand the topic) you mean the key. Just suggesting using "key" in the answer for clarity. BTW - concrete example - Visual C++'s std::hash of textual keys combines 10 characters evenly spaced along the text into the hash value, so it's O(1) regardless of text length (but massively more collision prone than GCC!). Separately, claims of O(1) have another assumption (normally correctly) that m is much less than n. Commented Feb 20, 2018 at 14:26