TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

Load factor must be less than or equal to 1

Error message

Load factor must be less than or equal to 1

What it means

The HashTable constructor throws ArgumentOutOfRangeException when loadFactor exceeds 1. This implementation resizes only when count >= threshold, so a load factor above 1 would allow the table to exceed capacity before resizing, degrading (or breaking) the invariant that entries fit in the backing array.

Solutions

  1. Pass a load factor in the range (0, 1]; use 0.75 as a sensible default.
  2. Convert percentage config values: loadFactor = percentValue / 100.0.
  3. Clamp before constructing: Math.Min(1.0, Math.Max(0.01, value)).

Example fix

// before
var ht = new HashTable<string, int>(16, 75); // percentage, > 1
// after
var ht = new HashTable<string, int>(16, 75 / 100.0); // 0.75
Defensive patterns

Strategy: validation

Validate before calling

if (loadFactor > 1)
    throw new ArgumentOutOfRangeException(nameof(loadFactor), "Load factor must be <= 1");

Type guard

static bool IsValidLoadFactor(double lf) => !double.IsNaN(lf) && lf > 0 && lf <= 1;

Try / catch

try { var ht = new HashTable<string,int>(cap, loadFactor); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "loadFactor")
{
    loadFactor = Math.Min(loadFactor / 100.0, 1.0); // maybe a percentage
}

Prevention

When it happens

Trigger: Calling new HashTable<TKey,TValue>(capacity, loadFactor) with loadFactor > 1, e.g. new HashTable<string,int>(10, 1.5) or a percentage supplied as 75 instead of 0.75.

Common situations: Config expressing load factor as a percentage (75) instead of a fraction (0.75); unit-of-measure confusion after porting from libraries that accept load factors > 1; typos like 10 instead of 1.0.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/6c0df8a1b4bbbc36. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/Hashing/HashTable.cs:105

    /// <paramref name="capacity"/> is rounded to the next prime number.
    /// </remarks>
    /// <see cref="PrimeNumber.NextPrime(int, int, bool)"/>
    /// <see cref="PrimeNumber.IsPrime(int)"/>
    public HashTable(int capacity = DefaultCapacity, float loadFactor = DefaultLoadFactor)
    {
        if (capacity <= 0)
        {
            throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than 0");
        }

        if (loadFactor <= 0)
        {
            throw new ArgumentOutOfRangeException(nameof(loadFactor), "Load factor must be greater than 0");
        }

        if (loadFactor > 1)
        {
            throw new ArgumentOutOfRangeException(nameof(loadFactor), "Load factor must be less than or equal to 1");
        }

        this.capacity = PrimeNumber.NextPrime(capacity);
        this.loadFactor = loadFactor;
        threshold = (int)(this.capacity * loadFactor);
        entries = new Entry<TKey, TValue>[this.capacity];
    }

    /// <summary>
    /// Adds a key-value pair to the hash table.
    /// </summary>
    /// <param name="key">Key to add.</param>
    /// <param name="value">Value to add.</param>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="key"/> is null.</exception>
    /// <exception cref="ArgumentException">Thrown when <paramref name="key"/> already exists in the hash table.</exception>
    /// <remarks>
    /// If the number of elements in the hash table is greater than or equal to the threshold, the hash table is resized.
    /// </remarks>

View on GitHub (pinned to 96e2905cab)