C# Program to Remove Duplicate Elements From Sorted Array

This program is all about how to remove duplicate elements from a sorted array in C#.

How to Remove Duplicate Elements from a Sorted Array

What we actually want For example- if we have an array of size 5 i.e arr[5] and in this array we have five elements that are {1,2,2,3,3} that are already in sorted manner in increasing order so we wanted to remove the elements from it which are repeated or duplicate so if we remove the duplicate elements from arr[ ] so we get elements in last ={1,2,3}  and by removing the duplicate elements the new size of array is now arr[3].

Example:

Input  : arr[] = {1, 2, 2, 3, 3}
Output : arr[] = {1, 2, 3}
         new size = 3

Basic Algorithm:

  1. Create an array arr[ ] for input, and array temp[ ] for storing unique elements.
  2. Then traverse input array arr[ ] and copy unique elements one by one of arr[ ] to temp[ ]. Also keeping track of the count that how many unique elements are their, and count  them as j.
  3. Copy j elements from temp[ ] to arr[ ] and return j which is the new size of our array after removing duplicate elements.
using System;
 
class RemoveDuplicate 
{

 static int removeDuplicates(int []arr, int n)

    {

       if (n == 0 || n == 1)

            return n;

        int []temp = new int[n];

        int j = 0;
        for (int i = 0; i < n - 1; i++)

           {
             if (arr[i] != arr[i+1])

             temp[j++] = arr[i];

             temp[j++] = arr[n-1]; 
           }

        for (int i = 0; i < j; i++)

          {
           arr[i] = temp[i];
          }

       return j;  

   }

     
     public static void Main () 

    {
        int []arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};

        int n = arr.Length;

         n = removeDuplicates(arr, n);

        for (int i = 0; i < n; i++)
         {

            Console.Write(arr[i] + " ");
         }
    }

}

Output:

1 2 3 4 5

We are getting this output because our input was arr[ ]={1,2,2,3,4,4,4,5,5} when the function has removing the duplicate values and after that the unique elements are left arr[ ]={1,2,3,4,5}.

Working:

We made a function to remove duplicate elements. This function returns new size of the modified array in last which is j . The if condition will return n if array is empty or if it contains a single element.
Then initialise new array temp[ ] and give its size belonging to n. Then we have taken one for loop declare j=0 and i=0 and run the loop till i is smaller than n-1.

It will start traversing elements in this for loop give if statement which check if current element is not equal to next element then store that current element store the last element as whether it is unique or repeated because it hasn’t stored previously.

Now Modify the original array by storing the temp[ ] elements in arr[ ] with the help of for loop which run till the i is smaller than j (j is count of the unique elements) , After all that print the updated array.

So in this way we can remove the duplicate elements from an sorted array in C#.