How to Remove Duplicate Values from an Array in C# – 6 Best Approaches with Examples
Removing duplicate values from an array is one of the most common tasks in C# programming. Whether you're processing user input, reading data from a database, or preparing data for reports, eliminating duplicate values improves data quality and application performance.
In this article, we'll explore multiple ways to remove duplicate values from an array in C#, ranging from beginner-friendly approaches to optimized solutions suitable for production applications and technical interviews.
Problem Statement
Suppose we have the following array:
int[] iArray = new int[] { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
Expected Output
1, 2, 3, 4, 5, 6
Method 1: Using LINQ Distinct() (Recommended)
The easiest and most readable approach is to use LINQ's Distinct() method.
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] iArray = { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
int[] uniqueArray = iArray.Distinct().ToArray();
Console.WriteLine(string.Join(", ", uniqueArray));
}
}
Output
1, 2, 3, 4, 5, 6
Advantages
Very easy to write
Highly readable
Excellent for production code
Preserves the first occurrence order
Disadvantages
Requires LINQ
Slightly more memory usage than in-place algorithms
Time Complexity: O(n)
Space Complexity: O(n)
Method 2: Using HashSet (Fastest)
A HashSet<T> stores only unique values. Any duplicate values are automatically ignored.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] iArray = { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
HashSet<int> uniqueNumbers = new HashSet<int>(iArray);
foreach (int item in uniqueNumbers)
{
Console.Write(item + " ");
}
}
}
Output
1 2 3 4 5 6
Advantages
Extremely fast
Automatically removes duplicates
Excellent for large collections
Disadvantages
Uses additional memory
Order is not guaranteed in all scenarios
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using List Without LINQ
This approach is useful for beginners or interview situations where LINQ is not allowed.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] iArray = { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
List<int> result = new List<int>();
foreach (int item in iArray)
{
if (!result.Contains(item))
{
result.Add(item);
}
}
Console.WriteLine(string.Join(", ", result));
}
}
Advantages
Easy to understand
No LINQ dependency
Disadvantages
Contains()performs a linear searchNot suitable for large datasets
Time Complexity: O(n²)
Method 4: Using Dictionary
A dictionary can also be used to keep track of values that have already been processed.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] iArray = { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
Dictionary<int, bool> dictionary = new Dictionary<int, bool>();
foreach (int item in iArray)
{
if (!dictionary.ContainsKey(item))
{
dictionary.Add(item, true);
}
}
foreach (var item in dictionary.Keys)
{
Console.Write(item + " ");
}
}
}
Advantages
Fast lookup
Good alternative to
HashSet
Disadvantages
Stores unnecessary values (
bool)More verbose than
HashSet
Time Complexity: O(n)
Method 5: Using Nested Loops (Without Collections)
This method is frequently asked during coding interviews because it demonstrates your understanding of the logic without relying on built-in collection classes.
using System;
class Program
{
static void Main()
{
int[] iArray = { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
for (int i = 0; i < iArray.Length; i++)
{
bool duplicate = false;
for (int j = 0; j < i; j++)
{
if (iArray[i] == iArray[j])
{
duplicate = true;
break;
}
}
if (!duplicate)
{
Console.Write(iArray[i] + " ");
}
}
}
}
Advantages
No LINQ
No collections
Demonstrates algorithmic thinking
Disadvantages
Slow for large datasets
Time Complexity: O(n²)
Space Complexity: O(1)
Method 6: Remove Consecutive Duplicates from a Sorted Array
If the array is already sorted, you only need to compare each element with the previous one.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] iArray = { 1, 1, 2, 2, 3, 3, 4, 5, 6, 6 };
List<int> result = new List<int>();
if (iArray.Length > 0)
{
result.Add(iArray[0]);
for (int i = 1; i < iArray.Length; i++)
{
if (iArray[i] != iArray[i - 1])
{
result.Add(iArray[i]);
}
}
}
Console.WriteLine(string.Join(", ", result));
}
}
Advantages
Very fast for sorted arrays
Simple implementation
Disadvantages
Works correctly only when duplicates are adjacent
Time Complexity: O(n)
Performance Comparison
| Method | Time Complexity | Space Complexity | Best Use Case |
|---|---|---|---|
| LINQ Distinct() | O(n) | O(n) | General-purpose applications |
| HashSet | O(n) | O(n) | Large datasets and high performance |
| List + Contains() | O(n²) | O(n) | Learning and small datasets |
| Dictionary | O(n) | O(n) | Alternative to HashSet |
| Nested Loops | O(n²) | O(1) | Coding interviews |
| Sorted Array Comparison | O(n) | O(n) | Already sorted arrays |
Real-World Use Cases
Removing duplicates is useful in many real-world applications, including:
Removing duplicate customer IDs
Eliminating repeated product codes
Cleaning imported CSV or Excel data
Processing unique email addresses
Generating distinct reports
Removing duplicate API response records
Preparing analytics data
Data migration and ETL processes
Interview Questions
1. What is the easiest way to remove duplicates from an array in C#?
Use Distinct() from LINQ.
2. Which collection automatically removes duplicates?
HashSet<T>.
3. What is the complexity of Distinct()?
Time Complexity: O(n)
Space Complexity: O(n)
4. Can we remove duplicates without using LINQ?
Yes. We can use:
HashSet
Dictionary
List
Nested loops
5. Which approach is preferred in production?
For most applications:
Distinct()for readabilityHashSet<T>for maximum performance
6. Which approach is commonly asked in coding interviews?
The nested-loop solution is a popular interview question because it tests your understanding of algorithms without relying on built-in methods.
Best Practices
Use
Distinct()for clean, readable, and maintainable code.Use
HashSet<T>when performance is critical.Avoid
List.Contains()for large datasets because it results in quadratic time complexity.Understand the manual nested-loop solution for coding interviews.
Choose the approach that best matches your application's performance and readability requirements.
Conclusion
Removing duplicate values from an array is a common programming task in C#. The language offers several approaches, each with its own strengths.
Use
Distinct()for simplicity and maintainability.Use
HashSet<T>for the best performance with large collections.Learn the nested-loop solution to strengthen your problem-solving skills for interviews.
Select the right approach based on your application's size, performance requirements, and coding standards.
Mastering these techniques will help you write cleaner, more efficient C# code and prepare you for technical interviews with confidence.
If you'd like, I can also create:
an SEO-optimized version (targeting keywords like "Remove Duplicates from Array in C#"),
a professional feature image/thumbnail,
and schema-ready FAQ markup to improve your blog's Google search visibility.
No comments:
Post a Comment