liveBook · Manning

Hashset

As explained in Fabulous Adventures in Data Structures and Algorithms

HashSet<T> is a .NET collection type that stores unique values and supports hash-based membership and lookup operations.

A HashSet<T> stores unique values and makes membership tests efficient by hashing each value into buckets. For a string of length m, hashing costs O(m), largely independent of the number of stored values. section 5.1.1

Its strengths and limits become clear when searching a large word list:

  • Use it for Contains-style membership tests, where hash-based lookup is effective. section 5.1.1
  • Do not use it for StartsWith searches: hashing deliberately distributes dissimilar words, providing no efficient prefix-search path. section 5.1.1
  • Treat equality and hashing as a pair: equivalent values must produce equal hash codes. section 5.4.2

Mutable values can disappear

If a mutable object is stored in a hash set and later changed in a way that changes its hash code, the set may no longer find it. Immutable values, or carefully controlled mutation, preserve the lookup invariant. section 5.4.2

Listing 5.6 Computing Node equivalence with an equality comparer
sealed class DawgBuilder
{
    private class NodeEquality : IEqualityComparer<Node>
    {
        public bool Equals(Node? x, Node? y) 
        {
            if (ReferenceEquals(x, y))
                return true;
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
                return false;
            if (x.IsEnd != y.IsEnd || x.Count != y.Count)
                return false;
            return x.EdgeLabels.All(e => 
                y.HasEdge(e) && 
                ReferenceEquals(x.FollowEdge(e), y.FollowEdge(e)));
        }

Inspect the C# equality comparer pattern used for structurally equivalent nodes, including reference checks, null handling, and edge comparison. section 5.4.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Explains hashing, buckets, complexity, equality contracts, and mutation hazards, but stops short of implementing the collection itself.
Examples
Uses large word lists, Contains, StartsWith, and structurally equivalent mutable graph nodes as examples.
What it assumes
Assumes familiarity with strings, collections, object mutation, and C# equality comparers.
What you can do after
Evaluate hash sets for membership workloads and implement compatible equality and hashing for stored objects.
How it connects
Connects hash sets with dictionaries, equality comparers, tries, prefix trees, and Directed Acyclic Word Graphs.

Also explained in

Compare how 2 books explain hashset →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.