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
- Ensure both arrays contain the same number of participants before calling Match.
- Re-check the loading/filtering logic that produced unequal counts.
- 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
- Load both participant groups with one shared query/filter
- Assert equal counts in tests with representative data
- Keep proposers and accepters in one paired data structure
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
- The step cannot be smaller than 1
- The step cannot be greater than the size of the group
- n
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
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)