Ad
  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    A set is effectively just a hash table, so it is average-case O(1) and worst-case O(n).

    The actual time taken to determine if an item is in the hash depends on how many buckets are used for the table, and how many items are in the bucket.

    If we had one bucket, or by sheer coincedence all of our items ended up in the same bucket, we'd have O(n) complexity. We would have to iterate through the bucket until we found our item. This is our worst-case scenario.
    If we had x buckets, and we assume that we've got some pretty uniform distribution between them, we'd have O(1) overall complexity, and a O(n) complexity for the bucket your item is in. That is still O(1) complexity however.

    You can think of putting an item in a bucket as:

    hash = h(item)
    buckets[hash % total_buckets].push(item)
    

    and then determining if an item is in a bucket as:

    bucket = buckets[hash]
    return any(x == item for x in bucket)
    

    *Disclaimer that this is simplified to hell and back