C# List Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
Here are some examples of working with lists in C# and removing duplicates:
List<int> numbers = new List<int> { 1, 2, 3, 2, 4, 3, 5, 1 }; var uniqueNumbers = numbers.Distinct(); foreach (var number in uniqueNumbers) { Console.WriteLine(number); }
In this example, a List<int>
is initialized with several integer values, some of which are duplicated. The Distinct
method is used to obtain a unique list of integers, and then the resulting values are printed to the console using a foreach
loop.
List<int> numbers1 = new List<int> { 1, 2, 3, 4 }; List<int> numbers2 = new List<int> { 3, 4, 5, 6 }; var mergedNumbers = numbers1.Union(numbers2); foreach (var number in mergedNumbers) { Console.WriteLine(number); }
In this example, two List<int>
objects are initialized with some overlapping integer values. The Union
method is used to merge the lists into a single list, removing any duplicates. The resulting merged list is then printed to the console using a foreach
loop.
List<int> numbers1 = new List<int> { 1, 2, 3, 4 }; List<int> numbers2 = new List<int> { 3, 4, 5, 6, 6 }; var difference = numbers1.Concat(numbers2).GroupBy(x => x).Where(g => g.Count() == 1).Select(g => g.Key); foreach (var number in difference) { Console.WriteLine(number); }
In this example, two List<int>
objects are initialized with some overlapping integer values. The Concat
method is used to combine the lists into a single list, and then the GroupBy
method is used to group the items by value. The Where
method is then used to select only items that occur once in the combined list, preserving any duplicates. The resulting list of unique items is then printed to the console using a foreach
loop.
C# remove duplicates from List:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; List<int> uniqueNumbers = numbers.Distinct().ToList();
Remove duplicate items from List in C#:
List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" }; List<string> uniqueColors = colors.Distinct().ToList();
Removing duplicates from List using LINQ in C#:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; List<int> uniqueNumbers = numbers.Distinct().ToList();
Remove duplicate strings from List in C#:
List<string> names = new List<string> { "John", "Jane", "John", "Doe" }; List<string> uniqueNames = names.Distinct().ToList();
C# List remove duplicates with HashSet:
List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" }; HashSet<string> uniqueColors = new HashSet<string>(colors); List<string> uniqueColorsList = new List<string>(uniqueColors);
Remove duplicate objects from List in C#:
List<Person> persons = new List<Person> { /* List of Person objects */ }; List<Person> uniquePersons = persons.Distinct().ToList();
C# List remove duplicate items by property:
List<Person> persons = new List<Person> { /* List of Person objects */ }; List<Person> uniquePersons = persons.GroupBy(p => p.Id).Select(group => group.First()).ToList();
Remove duplicate integers from List in C#:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; List<int> uniqueNumbers = numbers.Distinct().ToList();
C# List remove duplicates with Distinct:
List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" }; List<string> uniqueColors = colors.Distinct().ToList();
Remove duplicate elements from List with HashSet in C#:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; HashSet<int> uniqueNumbersSet = new HashSet<int>(numbers); List<int> uniqueNumbers = new List<int>(uniqueNumbersSet);
C# List remove duplicates ignoring case:
List<string> colors = new List<string> { "Red", "green", "Blue", "red", "Green" }; List<string> uniqueColors = colors.Select(c => c.ToLowerInvariant()).Distinct().ToList();
Remove duplicate elements from List using GroupBy in C#:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; List<int> uniqueNumbers = numbers.GroupBy(n => n).Select(group => group.Key).ToList();
C# List remove duplicates with custom equality comparer:
List<Person> persons = new List<Person> { /* List of Person objects */ }; List<Person> uniquePersons = persons.Distinct(new PersonEqualityComparer()).ToList();
Remove duplicates from List while preserving order in C#:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; List<int> uniqueNumbers = numbers.Distinct().ToList();
C# List remove duplicates and keep count:
List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" }; Dictionary<string, int> colorCount = colors.GroupBy(c => c).ToDictionary(group => group.Key, group => group.Count());
Remove duplicate elements from List with Dictionary in C#:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; Dictionary<int, bool> seen = new Dictionary<int, bool>(); List<int> uniqueNumbers = numbers.Where(n => seen.TryAdd(n, true)).ToList();
Remove duplicate items from List without LINQ in C#:
List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" }; List<string> uniqueColors = RemoveDuplicates(colors); // Custom method to remove duplicates List<T> RemoveDuplicates<T>(List<T> inputList) { HashSet<T> uniqueElements = new HashSet<T>(); List<T> result = new List<T>(); foreach (var item in inputList) { if (uniqueElements.Add(item)) { result.Add(item); } } return result; }
C# List remove duplicates with loop and HashSet:
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 }; HashSet<int> uniqueNumbersSet = new HashSet<int>(); List<int> uniqueNumbers = new List<int>(); foreach (var number in numbers) { if (uniqueNumbersSet.Add(number)) { uniqueNumbers.Add(number); } }