{"record":{"id":"acee3cd94631d127","repo":"louthy/language-ext","slug":"an-element-with-the-same-key-already-exists-in-the-set","errorCode":null,"errorMessage":"An element with the same key already exists in the set","messagePattern":"An element with the same key already exists in the set","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs","lineNumber":989,"sourceCode":"    [Pure]\n    public static SetItem<K> Add<OrdK, K>(SetItem<K> node, K key) where OrdK : Ord<K>\n    {\n        if (node.IsEmpty)\n        {\n            return new SetItem<K>(1, 1, key, SetItem<K>.Empty, SetItem<K>.Empty);\n        }\n        var cmp = OrdK.Compare(key, node.Key);\n        if (cmp < 0)\n        {\n            return Balance(Make(node.Key, Add<OrdK, K>(node.Left, key), node.Right));\n        }\n        else if (cmp > 0)\n        {\n            return Balance(Make(node.Key, node.Left, Add<OrdK, K>(node.Right, key)));\n        }\n        else\n        {\n            throw new ArgumentException(\"An element with the same key already exists in the set\");\n        }\n    }\n\n    [Pure]\n    public static SetItem<K> TryAdd<OrdK, K>(SetItem<K> node, K key) where OrdK : Ord<K>\n    {\n        if (node.IsEmpty)\n        {\n            return new SetItem<K>(1, 1, key, SetItem<K>.Empty, SetItem<K>.Empty);\n        }\n        var cmp = OrdK.Compare(key, node.Key);\n        if (cmp < 0)\n        {\n            return Balance(Make(node.Key, TryAdd<OrdK, K>(node.Left, key), node.Right));\n        }\n        else if (cmp > 0)\n        {\n            return Balance(Make(node.Key, node.Left, TryAdd<OrdK, K>(node.Right, key)));","sourceCodeStart":971,"sourceCodeEnd":1007,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Set/Internal/Set.Internal.cs#L971-L1007","documentation":"This ArgumentException is thrown by Set.Internal's Add<OrdK, K> when the key being inserted compares equal to an existing node's key. Sets cannot hold duplicates and, unlike the map variant, there is no TryAdd/TryUpdate option here, so a duplicate key is always rejected. The faulting input is the duplicate key argument to Add.","triggerScenarios":"Set.Add / set-cons operations where Ord.Compare(existing, key) == 0, e.g. re-adding an element or case-insensitive duplicates colliding under the comparer.","commonSituations":"Bulk-loading data with duplicate values, or wrong Ord type argument (e.g. OrdString.CaseInsensitive vs OrdString.Ordinal) causing unexpected collisions.","solutions":["De-duplicate the source sequence before adding (Distinct with the same comparer)","Use TryAdd or check Contains first","Verify the Ord instance used matches the intended equality semantics"],"exampleFix":"// before\nset = items.Fold(set, (s, x) => s.Add(x)); // throws on duplicates\n// after\nset = items.Distinct().Fold(set, (s, x) => s.Add(x));","handlingStrategy":"validation","validationCode":"if (!set.Contains(key)) set = set.Add(key);","typeGuard":null,"tryCatchPattern":"try { set = Set.add(key, set); }\ncatch (ArgumentException) { /* duplicate - ignore or log */ }","preventionTips":["Distinct() source collections before bulk Add","Confirm the Ord type parameter matches insertion-time comparer"],"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"}