In previous article, I mentioned Get Character to ASCII value in C# but in this article, I have provided working console application examples to remove duplicates from an array in C# using various possible ways.

Using Linq

When it comes to solve any problem in C#, Linq is always there, using Linq, you can easily remove duplicates from an array in C#, let's take a look at an console application example.

using System;
using System.Linq;

namespace RemoveDuplicates
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] strArray = { "11", "44", "33", "11", "55", "55"};
            strArray = strArray.Distinct().ToArray();
            foreach(var str in strArray)
            {
                Console.WriteLine(str);
            }
        }
    }
}

Output:

11
44
33
55

remove-duplicates-from-array-csharp

As you can see in the above approach, we are simply taking a string array and calling "Distinct()" method to remove duplicates from an array and then loop each string of an array to get distinct values.

Using HashSet

When it comes to removing duplicates from an array, there is another simple approach to use HashSet

using System;
using System.Collections.Generic;

namespace RemoveDuplicates
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] strArray = { "11", "44", "33", "11", "55", "55"};
            HashSet<string> set = new HashSet<string>(strArray);
            string[] result = new string[set.Count];
            set.CopyTo(result);
            foreach (var str in result)
            {
                Console.WriteLine(str);
            }
        }
    }
}

Output is the same as above:

11
44
33
55

If you will notice code in this approach, we are using 2 Array to get Distinct Array results.

One is to store the initial duplicate array and another is to store CopyTo Results.

Using ArrayList

In this approach, we will create an ArrayList from an array and before adding new element to ArrayList from Array, we will check if item exists in ArrayList using .Contains() method, if it already exists, we will skip the item else we will add it in ArrayList.

using System;
using System.Collections;

namespace RemoveDuplicates
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] strArray = { "11", "44", "33", "11", "55", "55"};
            var sList = new ArrayList(); //create a blank ArrayList

            for (int i = 0; i < strArray.Length; i++)
            {
                //check if item exists in List
                // Contains = true, if item Exists
                if (!sList.Contains(strArray[i]))
                {
                    //add item in arrayList if it doesn't exists
                    sList.Add(strArray[i]);
                }
            }

            //convert to Array again
            var sNew = sList.ToArray();

            //print array items
            for (int i = 0; i < sNew.Length; i++)
            {
                Console.WriteLine(sNew[i]);
            }
        }
    }
}

Output:

11
44
33
55

I have explained most of the code using comments in above C# program.

We are following below steps:

  • Create a blank ArrayList
  • Loop through each item of an array, using Contains() check if item already exists in ArrayList
  • If not, add item in ArrayList
  • Convert ArrayList to Array using ToArray()
  • Loop through each item to print Distinct values.

That's it, hope it helps.

You may also like to read:

How to add a item or multiple items in C# List

Connect to SQL Server in C# (example using Console application)

Understanding Unit testing in C# With Example

Use of Ajax.BeginForm in ASP.NET MVC C#

Program to remove duplicate elements in an array in Java

Declare and initialize Array in java