TechieClues . TechieClues .
Updated date Sep 14, 2022
In this blog, we will learn how to remove duplicate values from an array using C#.

In C#, we will easily remove the duplicate values using the Distinct() method. The method will not actually remove the values from the list but using the Distinct() method, we can distinct the value and assign it to another array to remove the duplicate values.

Remove Duplicate Values from the Integer or Number Array:

The below example shows how to get distinct values from an integer array using the Distinct() method and convert it to an array using ToArray() and assign it a new array. 

// Declare an array of integer type
int[] number = { 25, 15, 20, 40, 25, 32, 40, 85, 68, 25 };

// Use distinct() function to distinct the values            
int[] distinctValues = number.Distinct().ToArray();

  Code Snippet:

using System;
using System.Linq;

namespace RemoveDuplicates
{
    class Program
    { 
        static void Main(string[] args)
        {
            // Declare an array of integer type
            int[] number = { 25, 15, 20, 40, 25, 32, 40, 85, 68, 25 };
            // Display the values before removing duplicates
            Console.WriteLine("Before Removing: ");
            Console.WriteLine("----------------");
            Array.ForEach(number, x => Console.WriteLine(x));

            // Use distinct() function to distinct the values            
            int[] distinctValues = number.Distinct().ToArray();

            // Display the values after removing the duplictes
            Console.WriteLine("After Removing: ");
            Console.WriteLine("----------------");
            Array.ForEach(distinctValues, y => Console.WriteLine(y));
            Console.ReadKey();
        }
    }
}

Output:

Before Removing:
----------------
25
15
20
40
25
32
40
85
68
25
After Removing:
---------------
25
15
20
40
32
85
68

Remove Duplicate Values from the String Array:

Similar to the above example. The below code shows how to get distinct values (or remove duplicates) from the string array using the Distinct() method.

// Declare an array of string type
string[] stringValues = { "Red", "Blue", "Pink", "Red", "Yellow", "Grey", "Pink", "White", "Red" };

// Use distinct() function to distinct the values            
string[] distinctValues = stringValues.Distinct().ToArray();

Code Snippet:

using System;
using System.Linq;

namespace RemoveDuplicates
{
    class Program
    { 
        static void Main(string[] args)
        {
            // Declare an array of string type
            string[] stringValues = { "Red", "Blue", "Pink", "Red", "Yellow", "Grey", "Pink", "White", "Red" };
            // Display the values before removing duplicates
            Console.WriteLine("Before Removing: ");
            Console.WriteLine("----------------");
            Array.ForEach(stringValues, x => Console.WriteLine(x));

            // Use distinct() function to distinct the values            
            string[] distinctValues = stringValues.Distinct().ToArray();

            // Display the values after removing the duplictes
            Console.WriteLine("After Removing: ");
            Console.WriteLine("----------------");
            Array.ForEach(distinctValues, y => Console.WriteLine(y));
            Console.ReadKey();
        }
    }
}

Output:

Before Removing:
----------------
Red
Blue
Pink
Red
Yellow
Grey
Pink
White
Red
After Removing:
----------------
Red
Blue
Pink
Yellow
Grey
White

ABOUT THE AUTHOR

TechieClues .
TechieClues .
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!