How to Remove Duplicate Elements from an Array in C#

How to Remove Duplicate Elements from an Array in C#

In this article, I am going to discuss How to Remove Duplicate Elements from an array in C# with two different approaches. Please read our previous article where we discussed how to find the angle between the hour and minute hands of a clock at any given time in C# with an example. This question can be asked in many different ways as follows:

  1. How to Remove Duplicate Elements from an Array?
  2. How to print the elements that occurred only once in the array?
  3. How to remove the elements which occurred more than once in an array?
How to Remove Duplicate Elements from an Array in C#

Explanation: We will be given an array that may have duplicate elements (the same element say 2 will be present in index 0 and 4 etc. multiple times). Our task will be to show as output only those elements which are present in the array only once (only at one position in the whole array).

Method1 to print the elements that occurred only once in the array:

Step1: We are going to use two for loops for finding the unique elements one by one. First for loop will trace the whole array once. And other for loop will be used to compare the current element or value of arr1 at index i which is currently held by our first for loop.

for (i = 0; i < arr1.Length; i++)
{
        for (j = 0; j < arr1.Length; j++)
       {
             //Comparison Code
       }
}

We will basically take elements one by one using our first for loop and then using other for loop we will compare other elements of an array that is the same element or not. If this condition satisfies, we will immediately break the loop and increment the value of I which will now denote the next element of our array.

Step2: After taking the first element at the i index and comparing it with all other elements how we will get to know that is repeated or not?

For that, we are going to use one if condition

if (arr1.Length == j)
{
          Console.Write(arr1[i] + ” “);
}

If j value will be equal to arr1.Length basically means that the second for loop has traced all the other elements.

C# Program to Remove Duplicate Elements from an Array
using System;
class Program
{
    static void Main (string[]args)
    {
        int i = 0, j = 0;
        int[] arr1 = new int[]{ 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
        for (i = 0; i < arr1.Length; i++)
        {
         for (j = 0; j < arr1.Length; j++)
         {
             if (i == j)
             continue;
             if (arr1[j] == arr1[i])
             break;
         }
         if (arr1.Length == j)
         {
             Console.Write (arr1[i] + " ");
         }
        }
    }
}

Output: 9 4

Time Complexity: O(n2)

Outer for loop will execute O(n) time and for each value of I inner loop will execute again O(n) time which is basically 0(n) * O(n) = O(n2).

Doubt Section

Why we have used this part of the code in our program

 if (i == j)
       continue;

This is because when we taking elements one by one using our first for loop and comparing it with another array elements, we need to make sure that the same index value denoted by i should not compare with the same index denoted by j.

Method 2 to Remove Duplicate Elements from an Array in C#:

In Method 1 we need to compare each element of the array with all other elements to check whether that element has occurred in another index or not in the given array.

This can be optimized by sorting the array. After sorting the elements, we just need to compare elements with their adjacent elements only. And this is how the complexity of this problem can be reduced from O(n2) to O(nlogn) because we don’t need to compare elements with all other elements.

Step1: Check for the first element if it is equal to or not equal to its succeeding element.
Step2: Now start for index 1 and compare with its adjacent elements (left and right elements) till the n-1 elements.
Step3: At last, we need to compare the last element with its preceding element.

C# Program to remove the elements which occurred more than once in an array
using System;
class UniqueElements
{
    static void Unique (int[]arr, int n)
    {
        // Sort the array
        Array.Sort (arr);

        // Check for first element
        if (arr[0] != arr[1])
            Console.Write (arr[0] + " ");

        //Check for elements less than n-1
        for (int i = 1; i < n - 1; i++)
            if (arr[i] != arr[i + 1] && arr[i] != arr[i - 1])
             Console.Write (arr[i] + " ");

        // Check for the last element
        if (arr[n - 2] != arr[n - 1])
            Console.Write (arr[n - 1] + " ");
    }
    public static void Main ()
    {
        int[] arr = { 7, 7, 8, 8, 9, 1, 1, 4, 2, 2 };
        int n = arr.Length;
        Unique(arr, n);
    }
}

Output: 9 4

Time Complexity: O(nlogn)

Doubt Section

Why we are comparing in three sections 1 element then middle elements and then the last element instead of only using one for loop? This is because in our comparison code we have taken arr[i-1] and arr[i+1] for comparing adjacent elements, right? And for the first value when the value of I will be zero pointing to 0 indexes in the array and if we apply arr[I-1] which will denote arr[-1] which will ultimately throw index out of bounds exception. The same way at last index when we will be arr[n] and if we apply [n+1] which will again throw the same exception.

In the next article, I am going to discuss Duck Number in C#. Here, in this article, I try to explain How to Remove Duplicate Elements from an array in C# with two different approaches and I hope you enjoy this article.