{"record":{"id":"7041a6689c87ff9c","repo":"TheAlgorithms/C-Sharp","slug":"collections-must-have-equal-count","errorCode":null,"errorMessage":"Collections must have equal count","messagePattern":"Collections must have equal count","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/StableMarriage/GaleShapley.cs","lineNumber":17,"sourceCode":"namespace Algorithms.Problems.StableMarriage;\n\npublic static class GaleShapley\n{\n    /// <summary>\n    ///     Finds a stable matching between two equal sets of elements (fills EngagedTo properties).\n    ///     time complexity: O(n^2), where n - array size.\n    ///     Guarantees:\n    ///     - Everyone is matched\n    ///     - Matches are stable (there is no better accepter, for any given proposer, which would accept a new match).\n    ///     Presented and proven by David Gale and Lloyd Shapley in 1962.\n    /// </summary>\n    public static void Match(Proposer[] proposers, Accepter[] accepters)\n    {\n        if (proposers.Length != accepters.Length)\n        {\n            throw new ArgumentException(\"Collections must have equal count\");\n        }\n\n        while (proposers.Any(m => !IsEngaged(m)))\n        {\n            DoSingleMatchingRound(proposers.Where(m => !IsEngaged(m)));\n        }\n    }\n\n    private static bool IsEngaged(Proposer proposer) => proposer.EngagedTo is not null;\n\n    private static void DoSingleMatchingRound(IEnumerable<Proposer> proposers)\n    {\n        foreach (var newProposer in proposers)\n        {\n            var accepter = newProposer.PreferenceOrder.First!.Value;\n\n            if (accepter.EngagedTo is null)\n            {","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/StableMarriage/GaleShapley.cs#L1-L35","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nif (proposers.Length != accepters.Length) throw new ArgumentException(...);\nGaleShapley.Match(proposers, accepters);\n// after\nif (proposers.Length != accepters.Length)\n    throw new ArgumentException($\"Counts differ: {proposers.Length} proposers vs {accepters.Length} accepters\");\nGaleShapley.Match(proposers, accepters);","handlingStrategy":"validation","validationCode":"if (proposers.Length != accepters.Length) throw new ArgumentException($\"proposers ({proposers.Length}) and accepters ({accepters.Length}) must have equal count\");","typeGuard":null,"tryCatchPattern":"try { GaleShapley.Match(proposers, accepters); }\ncatch (ArgumentException ex) { logger.LogError(ex, \"Participant counts differ\"); }","preventionTips":["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"],"tags":["argument-validation","csharp","algorithm"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}