TheAlgorithms/C-Sharp · error

Collections must have equal count

Error message

Collections must have equal count

What it means

GaleShapley.Match requires the proposers and accepters collections to be the same size, since each proposer must be matched one-to-one with an accepter. It throws ArgumentException when the array lengths differ.

Solutions

  1. Ensure both arrays contain the same number of participants before calling Match.
  2. Re-check the loading/filtering logic that produced unequal counts.
  3. If the problem genuinely has unequal sides, pad or subset explicitly rather than letting the throw happen.

Example fix

// before
if (proposers.Length != accepters.Length) throw new ArgumentException(...);
GaleShapley.Match(proposers, accepters);
// after
if (proposers.Length != accepters.Length)
    throw new ArgumentException($"Counts differ: {proposers.Length} proposers vs {accepters.Length} accepters");
GaleShapley.Match(proposers, accepters);
Defensive patterns

Strategy: validation

Validate before calling

if (proposers.Length != accepters.Length) throw new ArgumentException($"proposers ({proposers.Length}) and accepters ({accepters.Length}) must have equal count");

Try / catch

try { GaleShapley.Match(proposers, accepters); }
catch (ArgumentException ex) { logger.LogError(ex, "Participant counts differ"); }

Prevention

When it happens

Trigger: Calling Match with proposers.Length != accepters.Length, e.g. Match(new Proposer[3], new Accepter[4]) after loading the two groups from different data sources.

Common situations: Building the two arrays from separate files/database queries with inconsistent filters; one side deduplicated or partially loaded; off-by-one when constructing preference lists.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at Algorithms/Problems/StableMarriage/GaleShapley.cs:17

namespace Algorithms.Problems.StableMarriage;

public static class GaleShapley
{
    /// <summary>
    ///     Finds a stable matching between two equal sets of elements (fills EngagedTo properties).
    ///     time complexity: O(n^2), where n - array size.
    ///     Guarantees:
    ///     - Everyone is matched
    ///     - Matches are stable (there is no better accepter, for any given proposer, which would accept a new match).
    ///     Presented and proven by David Gale and Lloyd Shapley in 1962.
    /// </summary>
    public static void Match(Proposer[] proposers, Accepter[] accepters)
    {
        if (proposers.Length != accepters.Length)
        {
            throw new ArgumentException("Collections must have equal count");
        }

        while (proposers.Any(m => !IsEngaged(m)))
        {
            DoSingleMatchingRound(proposers.Where(m => !IsEngaged(m)));
        }
    }

    private static bool IsEngaged(Proposer proposer) => proposer.EngagedTo is not null;

    private static void DoSingleMatchingRound(IEnumerable<Proposer> proposers)
    {
        foreach (var newProposer in proposers)
        {
            var accepter = newProposer.PreferenceOrder.First!.Value;

            if (accepter.EngagedTo is null)
            {

View on GitHub (pinned to 96e2905cab)