{"record":{"id":"914de5a2bbba673e","repo":"louthy/language-ext","slug":"an-element-with-the-same-key-already-exists-in-the-map-set","errorCode":null,"errorMessage":"An element with the same key already exists in the Map","messagePattern":"An element with the same key already exists in the Map","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs","lineNumber":868,"sourceCode":"        else if (cmp > 0)\n        {\n            node.Right = Add<OrdK, K>(node.Right, key, option);\n            return Balance(node);\n        }\n        else if (option == AddOpt.TryAdd)\n        {\n            // Already exists, but we don't care\n            return node;\n        }\n        else if (option == AddOpt.TryUpdate)\n        {\n            // Already exists, and we want to update the content\n            node.Key = key;\n            return node;\n        }\n        else\n        {\n            throw new ArgumentException(\"An element with the same key already exists in the Map\");\n        }\n    }\n\n    public static SetItem<K> Balance<K>(SetItem<K> node)\n    {\n        node.Height = (byte)(1 + Math.Max(node.Left.Height, node.Right.Height));\n        node.Count = 1 + node.Left.Count + node.Right.Count;\n\n        return node.BalanceFactor >= 2\n                   ? node.Right.BalanceFactor < 0\n                         ? DblRotLeft(node)\n                         : RotLeft(node)\n                   : node.BalanceFactor <= -2\n                       ? node.Left.BalanceFactor > 0\n                             ? DblRotRight(node)\n                             : RotRight(node)\n                       : node;\n    }","sourceCodeStart":850,"sourceCodeEnd":886,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs#L850-L886","documentation":"This ArgumentException is the strict branch of Set.Internal's Add helper: when a key comparing equal to an existing node's key is added and the add option is neither TryAdd nor TryUpdate, insertion cannot proceed and it throws. The faulting input is the duplicate key passed to Add<OrdK, K> without a tolerant AddOpt; it enforces key uniqueness in the AVL tree structure.","triggerScenarios":"Calling the internal Add path (e.g. via Set.add on an existing element) with a key that compares equal to an existing element under the set's ordering Ord.","commonSituations":"Adding items from user input or a data source without checking Contains first, or an Ord implementation that treats distinct objects as equal (comparer returning 0).","solutions":["Check set.Contains(key) before adding, or use AddOrUpdate/TryAdd semantics","Use the returned set from Add and rely on TryAdd where available","Fix a buggy Ord comparer that collapses distinct values to equal"],"exampleFix":"// before\nset = set.Add(item); // throws if present\n// after\nif (!set.Contains(item)) set = set.Add(item);","handlingStrategy":"validation","validationCode":"if (set.Contains(key))\n    throw new InvalidOperationException($\"Item {key} already exists\");\nset = set.Add(key);","typeGuard":null,"tryCatchPattern":"try { set = set.Add(item); }\ncatch (ArgumentException) { /* item already present - skip or update */ }","preventionTips":["Check Contains before Add","De-duplicate bulk inputs with Distinct using the same comparer","Audit Ord implementations for unintended equality collapses"],"tags":["dotnet","set","duplicate-key","add"],"backgroundTag":"duplicate-key","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}