How to Remove Duplicate Values From an Array in C#? Last Updated : 04 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To remove duplicate values from an array in C#, you can use different approaches based on your requirements. So to do this we use the Distinct() function. HashSet, Using a Loop.This function gives distinct values from the given sequence. This method will throw ArgumentNullException if the given array is null. Example:Input: int[] arr = { 1, 2, 2, 3, 4, 4, 5 }Output: 1,2,3,4,5Table of Content1.Distinct() Function2.Using HashSet<T>Using a LoopChoose the approach based on readability, performance requirements, or your familiarity with C#. Generally, LINQ and HashSet approaches are more concise and easier to understand.1.Distinct() FunctionLINQ offers the Distinct() method to easily remove duplicates from an array.Syntax:array_name.Distinct()Example: C# //C# program that demonstrates how to retrieve unique values from an array using the Distinct method using System; using System.Linq; class Program { static void Main() { int[] arr = { 1, 2, 2, 3, 4, 4, 5 }; // Use Distinct to get unique values int[] distinct = arr.Distinct().ToArray(); foreach(int x in distinct) { Console.Write(x+" "); } } } Output1 2 3 4 5 Explanation:The Distinct() method removes duplicates.ToArray() converts the result back to an array.2.Using HashSet<T>The HashSet class automatically removes duplicates because it only allows unique values.Syntax:HashSet<int> s = new HashSet<int>(array);Example: C# //C# program that demonstrates how to retrieve unique values from an array using a HashSet. using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = { 1, 2, 2, 3, 4, 4, 5 }; HashSet<int> set = new HashSet<int>(arr); foreach (int num in set) { Console.Write(num + " "); } } } Output1 2 3 4 5 Explanation:A HashSet is created using the original array, which removes duplicates.The HashSet is then copied to a new array to maintain the array format.3.Using a LoopYou can use a loop to iterate through the array and add elements to a list if they haven’t already been added. C# //C# programs that demonstrate how to retrieve unique values from an array using a List using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = { 1, 2, 2, 3, 4, 4, 5 }; List<int> distinctList = new List<int>(); foreach (int num in arr) { if (!distinctList.Contains(num)) { distinctList.Add(num); } } foreach (int num in distinctList) { Console.Write(num + " "); } } } Output1 2 3 4 5 Explanation:Iterate through each element of the original array.Add it to the list only if it doesn’t already exist in the list (!uniqueList.Contains(value)).Finally, convert the list to an array using ToArray(). Comment More infoAdvertise with us Next Article How to Remove Duplicate Elements from an Array using Lodash ? 171fa07058 Follow Improve Article Tags : C# C# Programs CSharp-Arrays-Programs Similar Reads How to use Array.BinarySearch() Method in C# | Set -1 Array.BinarySearch() method is used to search a value in a sorted one dimensional array. The binary search algorithm is used by this method. This algorithm searches a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the 13 min read How to remove duplicate values from array using PHP? In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.Syntax:array array_unique($array, $sort_flags);Note: The keys of the array are preserved i 4 min read How to Remove Duplicates From Array Using VBA in Excel? Excel VBA code to remove duplicates from a given range of cells. In the below data set we have given a list of 15 numbers in âColumn Aâ range A1:A15. Need to remove duplicates and place unique numbers in column B. Sample Data: Cells A1:A15 Sample Data Final Output: VBA Code to remove duplicates and 2 min read How to Remove Duplicate Elements from an Array using Lodash ? Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array. Table of Content Using uniq methodUsing groupBy and map methodsUsing xor functionUsing un 3 min read How to Remove Duplicate Elements from Array in Ruby? This article focuses on discussing how to remove duplicate elements from an array in Ruby. In Ruby, an array is a fundamental data structure that stores a collection of elements in a specific order. Various methods and approaches can be used to remove duplicate elements from an array. Removing Dupli 2 min read How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table 2 min read Like