How to Remove Duplicate Elements from an Array in C#

In this C# prgramming example, we will learn how to remove duplicate elements from an array and print only unique elements from an array in C#.

Remove Duplicate Elements from an Array in C#

The problem is we will be given an array that may have duplicate elements (it means the same element can be present multiple times in an array). Our task is to write a C# program to remove duplicate elements from an array and print only unique elements.

Unique elements are present in the array only once (only at one position in the whole array).

For example-  The input array contains 11, 15, 24, 11, 24, 15, 24 elements, then unique elements in the give input array is 11, 15, 24.

Input:11, 15, 24, 11, 24, 15, 24 in an array
Output: 11, 15, 24 ( only unique elements)

We can get a solution to this problem with the help of two methods.

  •  By using Loops
  • By using HashSet

Remove Duplicate Elements from an Array in C# Using Loops

We will use two loops for finding the unique elements one by one and comparing the elements of an array that they have some element or not using if condition.

Algorithm

Step 1: Compare each element with the next of each element of this element.
step 2: If find a duplicate element then left shift all the elements after the duplicate element up-to array size to remove duplicate one.
step 3: In this way, we can remove duplicate elements from the same array and manage unique elements.

C# program to remove duplicate values from an array using two for loops

class PrintUniqueElements
 {

public static int printUniqueElements(int[] arr, int n)
   {

// Traversing each element
// in array one by one by outer for loop.
   for (int i = 0; i < n; i++)
   {

// Traversing starts from next
// element of marked element by outer loop.
    for (int j = i + 1; j < n; )
   {

// If get same element then enter here.
       
      if (arr[i] == arr[j])
   {
// Left shifting each element from
// this duplicate element upto
// maximum size of array element.
   for (int k = j; k < n - 1; k++)
    {
      arr[k] = arr[k + 1];
    }

// Decreasing the array index by 1.
     n--;

    }
     else
    {

// If dont get any duplicate then come here and
// go to next element by inner loop.
        j++;
      }
    }
  }

// Returning the final array size
// after removing duplicate elements.
       return n;

     }
  }

/*------------------------------------------
 * USER OR CLIENT PORTION.
 * 
 */

 public class RemoveDuplicatesFromArrayTest
    {
 public static void Main(String[] args) {
 
// Entering the size of the array.
 Console.WriteLine("Please enter the size of the array";
     int size;
 bool flag = int.TryParse(Console.ReadLine(), out size);

 if(flag==true)
 {
   if(size <= 0)
   {
Console.WriteLine("Array size cannot be Zero or Negative");
   }
 }
    flag = false;

// Declaring and instantiating an array.
    int[] arr = new int[size];
 
// Entering elements in the array
		
Console.WriteLine("Enter the elements to store in an array");

for (int i = 0; i < size; i++)
  {
flag = int.TryParse(Console.ReadLine(),out arr[i]);
   if(flag==false)
   {
     return;
   }
   flag = false;
 }
// Displaying the original array.
 Console.WriteLine("Original Array: ");
  for (int i = 0; i < arr.Length; i++)
 {
    Console.WriteLine(arr[i]);
 }
 
// Displaying the unique elements in the array.
Console.WriteLine("Print unique elements: ");
 
int newSize = PrintUniqueElements.printUniqueElements(arr, size);
 
for (int i = 0; i < newSize; i++) 
   {
    Console.WriteLine(arr[i]);
    }
  }
}

Output:

Please enter the size of the array

8

Enter the elements to store in an array

5

3

6

8

5

3

6

5

Original Array:

5

3

6

8

5

3

6

5

Print unique elements:

3

5

6

8

Remove duplicate values from an array in C# using HashSet

There is one more method for doing this work. We know that HashSet discards the duplicates. Here we will convert the given array (with duplicates) to a HashSet and then convert the HashSet back to the array. This will show the result in an array but without any duplicates. Please remind that the ordering of the elements will be destroyed when HashSet is used.

The below example shows how to use the HashSet to remove duplicates from an array.

C# program to remove duplicate values from an array using HashSet

using System;
using System.Collections.Generic;
 
public class Example
{
    public static T[] removeDuplicates(T[] array)
    {
        HashSet set = new HashSet(array);
        T[] result = new T[set.Count];
        set.CopyTo(result);
        return result;
    }
 
    public static void Main()
    {
        int[] array = { 2, 3, 3, 4, 1, 2, 5 };
        int[] distinct = removeDuplicates(array);
 
        Console.WriteLine(String.Join(",", distinct));
    }
}

Output:

2,3,4,1,5

So here you can see how we can remove the duplicate values from an array by different two methods it and print only the unique elements of an array in c#.