Remove List
Several methods of repeated items
One of the simplest and easy -to -read solutions is to use Linq (Language Integration). This feature is introduced in .NET 3, providing a statement method to query and convey data. The following is an example of linq:
Filter the repetitive elements in the
list, and return a new list<code class="language-csharp">List<T> withDupes = LoadSomeData(); List<T> noDupes = withDupes.Distinct().ToList();</code>
Methods transform the generated Distinct()
to withDupes
. noDupes
ToList()
If you are already using Linq expressions, this method is very convenient. IEnumerable
List
Another choice is to use HashSet, which is a collection type designed for fast search and efficient repeated detection. The following is the method you can perform this operation:
Initially empty. We traversed each project in and tried to add it to the collection. Since HashSet is not allowed to repeat, only the only project can be successfully added. Finally, the generated collection uses to return back to List.
This method provides O (N) performance for insertion and search operations.
<code class="language-csharp">HashSet<T> hashSet = new HashSet<T>(); foreach (var item in withDupes) { hashSet.Add(item); } List<T> noDupes = new List<T>(hashSet);</code>
HashSet hashSet
withDupes
The third method involves the use of Dictionary to track the unique element: noDupes
Temporary storage for the unique element. We traversed the list to check whether each item exists in the dictionary. If not, we add it to the dictionary and
list.This method also provides O (N) complexity, but it needs to create additional data structures for tracking uniquely.
Select the right method
<code class="language-csharp">Dictionary<T, bool> dict = new Dictionary<T, bool>(); List<T> noDupes = new List<T>(); foreach (var item in withDupes) { if (!dict.ContainsKey(item)) { dict.Add(item, true); noDupes.Add(item); } }</code>
The above is the detailed content of How Can I Efficiently Remove Duplicates from a C# List?. For more information, please follow other related articles on the PHP Chinese website!