C#Array Deletion Cheats: Quick Deletion Tips reveal to make your code more efficient!

C#Array Deletion Cheats: Quick Deletion Tips reveal to make your code more efficient!

When it comes to deleting elements in a C#array, you may encounter two common methods: regular deletes and swap deletes (quick deletes). Normal deletes require traversing the array and moving elements, while swap deletes delete by swapping element positions. This article describes the time complexity of these two methods and provides sample code to demonstrate their use. By learning these quick removal techniques, you will be able to optimize your code to make it more efficient. Let's reveal these tips together to make your code better!

最后更新 11/11/2023 6:11 PM
沙漠尽头的狼
预计阅读 4 分钟
分类
.NET
标签
.NET C# algorithm

introduction

In C#, deleting elements in an array is a common operation. This article will introduce two common deletion methods: regular deletion and swap deletion (quick deletion). We will compare their time complexity and provide sample code to demonstrate their use.

General Deletion

Regular deletion means deleting a specified element by traversing the array and moving the element. The time complexity of this method is O(n), where n does not refer to the length of the array, and varies depending on the position of the element that needs to be deleted. After deleting a specified array element, the time complexity of the deletion operation is high because the following elements need to be moved forward.

The following is an example code for a general deletion:

int[] array = new int[] { 1, 2, 3, 4, 5 };
int index = 2; // 需要删除的元素的索引

for (int i = index; i < array.Length - 1; i++)
{
    array[i] = array[i + 1];
}

Array.Resize(ref array, array.Length - 1);

foreach (int element in array)
{
    Console.WriteLine(element);
}

The output results are:

1
2
4
5

Swap Delete (Quick Delete)

Swap delete is a method of deleting elements in an array by swapping element positions. The specific steps are as follows:

  1. Swap the element that needs to be deleted with the last element of the array.
  2. Delete the last element of the array.

The time complexity of this method is O(1), because only one exchange and one deletion operation are needed. If only the last bit is deleted, then there is only one operation. 1 does not mean a fixed number of operations, but means that the number of operations is fixed regardless of the length of the array.

The following is an example code for swapping and deleting:

int[] array = new int[] { 1, 2, 3, 4, 5 };
int index = 2; // 需要删除的元素的索引

if (index < array.Length - 1)
{
    array[index] = array[array.Length - 1];
}

Array.Resize(ref array, array.Length - 1);

foreach (int element in array)
{
    Console.WriteLine(element);
}

The output results are:

1
2
5
4

summary

By comparing the time complexity of regular deletes and swap deletes (fast deletes), we can see that swap deletes methods are more efficient in most cases. Normal deletes require traversing the array and moving elements, with a time complexity of O(n), while swap deletes only require one swap and one delete operation, with a time complexity of O(1).

However, it should be noted that the swap delete method only works with unordered arrays, because swap operations change the relative order of elements. If the array is ordered, the swap delete method will destroy the order and require reordering the array.

此外,交换删除方法也不适用于需要保持数组连续性的情况,因为删除操作会导致数组的长度减小。如果需要保持数组的连续性,可以考虑使用其他数据结构,如列表(List<T>)或链表(LinkedList<T>)。

I hope this article will help you understand how to quickly delete elements in a C#array! If you have any questions or suggestions, please feel free to leave a message.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读