Keywords: C# | Array Deduplication | LINQ | HashSet | Algorithm Optimization
Abstract: This technical paper provides an in-depth examination of various approaches for removing duplicate elements from arrays in C#. Building upon high-scoring Stack Overflow answers and authoritative technical documentation, the article thoroughly analyzes three primary implementation methods: LINQ's Distinct() method, HashSet collections, and traditional loop iterations. Through detailed code examples and technical explanations, it offers comprehensive guidance for developers to select optimal solutions based on specific requirements.
Introduction
In C# programming practice, removing duplicate elements from arrays is a common requirement. While seemingly straightforward, different solutions exhibit significant variations in performance, readability, and applicable scenarios. This paper systematically analyzes three mainstream implementation methods based on high-quality Stack Overflow answers and technical documentation.
LINQ Distinct() Method
LINQ (Language Integrated Query) provides powerful data query capabilities in C#, with the Distinct() method offering the most concise solution for duplicate removal.
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] originalArray = { 1, 2, 3, 3, 4 };
int[] distinctArray = originalArray.Distinct().ToArray();
foreach (int element in distinctArray)
{
Console.Write(element + " ");
}
}
}
Code Analysis: The Distinct() method returns an IEnumerable<T> sequence containing unique elements from the source. Calling ToArray() converts the result back to array format. This approach has O(n) time complexity and O(n) space complexity, demonstrating good performance in most scenarios.
HashSet Collection Approach
HashSet<T> is specifically designed for storing unique elements, with internal implementation based on hash tables that automatically remove duplicates.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] originalArray = { 1, 2, 2, 3, 4, 4, 5 };
HashSet<int> uniqueSet = new HashSet<int>(originalArray);
foreach (int number in uniqueSet)
{
Console.Write(number + " ");
}
}
}
Technical Details: The HashSet constructor accepts an IEnumerable<T> parameter and automatically filters duplicates during construction. This method has approximately O(n) time complexity, though actual performance may be slightly lower than LINQ due to hash collision handling.
Traditional Loop Iteration Method
For scenarios requiring fine-grained control or specific business logic, traditional loop iterations combined with lists can implement deduplication functionality.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] originalArray = { 1, 2, 2, 3, 4, 4, 5 };
List<int> distinctList = new List<int>();
foreach (int element in originalArray)
{
if (!distinctList.Contains(element))
{
distinctList.Add(element);
}
}
foreach (int number in distinctList)
{
Console.Write(number + " ");
}
}
}
Performance Analysis: This approach has O(n²) time complexity because the List.Contains() method needs to traverse the entire list during each iteration. While simple and intuitive to implement, it performs poorly with large datasets.
Method Comparison and Selection Guidelines
Each method has distinct advantages: LINQ offers concise code and excellent readability, suitable for most business scenarios; HashSet provides stable performance, particularly ideal for subsequent collection operations; loop iteration, despite lower performance, offers maximum flexibility.
In practical development, the LINQ method is recommended as the first choice, unless there are explicit performance optimization requirements or special business logic. For large datasets, consider using HashSet for better performance.
Exception Handling and Edge Cases
All methods require handling cases where the input array is null. In practical applications, appropriate null checks should be added:
if (originalArray == null)
{
throw new ArgumentNullException(nameof(originalArray));
}
Additionally, for custom object arrays, ensure proper implementation of Equals() and GetHashCode() methods; otherwise, deduplication logic may not work as expected.
Conclusion
C# provides multiple solutions for removing duplicate elements from arrays, allowing developers to select the most appropriate method based on specific requirements. LINQ's Distinct() method stands as the preferred choice due to its conciseness and good performance, while HashSet and traditional loop methods have their application value in specific scenarios. Understanding the internal implementation principles and performance characteristics of various methods enables more informed technical decisions in practical development.