{"record":{"id":"710f6bc969955423","repo":"TheAlgorithms/C-Sharp","slug":"element-not-in-heap","errorCode":null,"errorMessage":"{element} not in heap!","messagePattern":"(.+?) not in heap!","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/BinaryHeap.cs","lineNumber":154,"sourceCode":"    public bool Contains(T element) => data.Contains(element);\n\n    /// <summary>\n    ///     Remove an element from the heap.\n    /// </summary>\n    /// <remarks>\n    ///     In removing an element from anywhere in the heap, we only need to push down or up\n    ///     the replacement value depending on how the removed value compares to its\n    ///     replacement value.\n    /// </remarks>\n    /// <param name=\"element\">The element to remove from the heap.</param>\n    /// <exception cref=\"ArgumentException\">Thrown if element is not in heap.</exception>\n    public void Remove(T element)\n    {\n        var idx = data.IndexOf(element);\n\n        if (idx == -1)\n        {\n            throw new ArgumentException($\"{element} not in heap!\");\n        }\n\n        Swap(idx, data.Count - 1);\n        var tmp = data[^1];\n        data.RemoveAt(data.Count - 1);\n\n        if (idx < data.Count)\n        {\n            if (comparer.Compare(tmp, data[idx]) > 0)\n            {\n                HeapifyDown(idx);\n            }\n            else\n            {\n                HeapifyUp(idx);\n            }\n        }\n    }","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/BinaryHeap.cs#L136-L172","documentation":"BinaryHeap.Remove(element) throws ArgumentException with \"{element} not in heap!\" when IndexOf(element) returns -1, i.e. the value is not present (which includes an empty heap). The library refuses to remove absent items.","triggerScenarios":"Calling Remove() with a value never pushed, a value already removed, or on an empty heap (Index -1).","commonSituations":"Removing an item twice, removing a struct/equality-mismatched value, or removing from a heap built with a custom comparer where equality assumptions differ.","solutions":["Check heap.Contains(element) (Count > 0 and IndexOf != -1) before Remove().","Catch ArgumentException around Remove() if absence is expected.","Verify the element's Equals semantics match what was pushed (especially with custom comparers)."],"exampleFix":"// before\nheap.Remove(item);\n// after\nif (heap.Count > 0 && heap.Contains(item))\n{\n    heap.Remove(item);\n}","handlingStrategy":"validation","validationCode":"if (heap.Count > 0 && heap.Contains(element)) heap.Remove(element);","typeGuard":null,"tryCatchPattern":"try { heap.Remove(element); } catch (ArgumentException) { /* element absent; ignore or log */ }","preventionTips":["Check Contains before Remove","Avoid removing the same element twice","Be careful with custom comparers and element equality"],"tags":["data-structures","heap","argument"],"backgroundTag":"entity-not-found","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"}