How to delete an element from an array in C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In C#, arrays are fixed-size, so deleting an element always means creating a new array or switching to a mutable collection type. Many bugs come from expecting in-place shrink behavior that arrays do not support. The right strategy depends on whether removal is by index, by value, one-time, or repeated frequently.
Arrays Are Fixed-Length by Design
This is the key rule: T[] length cannot change after allocation. Removal therefore requires copying selected elements into a new array.
For frequent insert and remove operations, List<T> is often a better data structure.
Remove by Index With Manual Copy
Manual copy is explicit and easy to validate.
Remove by Index With Array.Copy
Array.Copy can be faster and concise for large arrays.
This avoids per-element branch checks in the copy loop.
Remove by Value
If removal is value-based, decide whether you want first match or all matches.
Remove all matches with LINQ:
Remove first match only:
Use List<T> for Repeated Mutations
When many removals happen over time, convert to List<T>.
This often simplifies logic and improves code maintainability.
Remove a Range of Elements
For batch deletions, range-based copy is more efficient than repeated single removals.
Range operations matter in data-cleanup and edit-buffer workflows.
Complexity and Allocation Tradeoffs
Any array deletion is at least O(n) due to copying. Frequent deletions also create many allocations and pressure garbage collection.
Guideline:
- Rare deletion: array copy approach is fine.
- Frequent mutable edits: use
List<T>. - Performance-critical hot path: benchmark copy strategy versus list workflow.
Common Pitfalls
- Expecting array length to shrink in place. Fix: create a new array or switch to
List<T>. - Forgetting index validation before removal. Fix: guard bounds and throw clear exceptions.
- Using LINQ in tight loops without considering allocations. Fix: prefer manual copy in performance-sensitive paths.
- Removing all matches when only first match should be removed. Fix: define removal contract explicitly.
- Repeated single deletions for contiguous ranges. Fix: use one range-removal copy operation.
Summary
- C# arrays are fixed-size, so deletion requires new allocation.
- Remove by index with manual loop or
Array.Copy. - Remove by value with clear first-match or all-match semantics.
- Prefer
List<T>for frequent mutable edits. - Choose the approach based on correctness contract and performance profile.
Related reading
- How to delete an item in a list if it exists?
- How to delete in a heap data structure?
- How to delete items from a dictionary while iterating over it?
- How to delete last item in list?
- How to deserialize a JObject to .NET object
- How to detect modifier key states in WPF?
- How to detect a Christmas Tree?
- How to detect a loop in a linked list?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.