Table of Contents

Class HashSet<T>

Namespace
AlgorithmsSW.Set
Assembly
AlgorithmsSW.dll

Represents a hash table that uses linear probing to resolve collisions.

[ExerciseReference(3, 4, 28)]
public class HashSet<T> : ISet<T>, IEnumerable<T>, IEnumerable

Type Parameters

T

The type of the keys in the hash table.

Inheritance
HashSet<T>
Implements
ISet<T>
Inherited Members
Extension Methods

Remarks

Linear probing is an open-addressing strategy where we look for the next available slot in the array when a collision occurs.

This version always have a prime table size, so the table hashing mechanism can work as expected.

Constructors

HashSet(IComparer<T>)

Initializes a new instance of the HashSet<T> class.

public HashSet(IComparer<T> comparer)

Parameters

comparer IComparer<T>

The comparer used to compare keys.

HashSet(int, IComparer<T>)

Initializes a new instance of the HashSet<T> class.

public HashSet(int initialCapacity, IComparer<T> comparer)

Parameters

initialCapacity int

The initial capacity of the hash table.

comparer IComparer<T>

The comparer used to compare keys.

Properties

Comparer

Gets the comparer used to compare items.

public IComparer<T> Comparer { get; }

Property Value

IComparer<T>

Count

Gets the number of items in the set.

public int Count { get; }

Property Value

int

Methods

Add(T)

Adds an item to the set.

public void Add(T key)

Parameters

key T

Contains(T)

Checks if the set contains an item.

public bool Contains(T key)

Parameters

key T

Returns

bool

true if the set contains the item, false otherwise.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate through the collection.

Remove(T)

Removes an item from the set.

public bool Remove(T key)

Parameters

key T

Returns

bool

true if the item was removed, false otherwise.