public bool Equals(SegmentedHashSet <T>?x, SegmentedHashSet <T>?y) { // If they're the exact same instance, they're equal. if (ReferenceEquals(x, y)) { return(true); } // They're not both null, so if either is null, they're not equal. if (x == null || y == null) { return(false); } var defaultComparer = EqualityComparer <T> .Default; // If both sets use the same comparer, they're equal if they're the same // size and one is a "subset" of the other. if (SegmentedHashSet <T> .EqualityComparersAreEqual(x, y)) { return(x.Count == y.Count && y.IsSubsetOfHashSetWithSameComparer(x)); } // Otherwise, do an O(N^2) match. // 🐛 This is non-symmetrical, but matches original: https://github.com/dotnet/runtime/issues/69218 foreach (var yi in y) { var found = false; foreach (var xi in x) { if (defaultComparer.Equals(yi, xi)) { found = true; break; } } if (!found) { return(false); } } return(true); }