Remove duplicate values from array in C Sharp

Learn how to remove duplicate elements in an array in C# with example.

For example, The input array contains 12, 14, 23, 12, 23, 14, 23 elements, then unique elements in the give input array is 12, 14, 23.

Input:12, 14, 23, 12, 23, 14, 23 in an array
Output: 12, 14, 23 ( only unique elements)

Algorithm

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

Method -1

c# program on removing duplicate elements from an array

Code

  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
6
7
8
5
6
7
5
Original Array:
5
6
7
8
5
6
7
5
Print unique elements:
5
6
7
8

Method -2

Code

using System;
using System.Collections.Generic;


namespace RemoveDuplicateValues_from_Array
{
    class Program
    {
        static void Main(string[] args)
        {


            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;

            int[] arr = new int[size];

            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: ");

            foreach(int num in arr)
            {
                Console.WriteLine(num);
            }

            List lstarray = new List(arr);

            RemoveDuplicates(ref lstarray);

            // Displaying the unique elements in the array.
            Console.WriteLine("Print unique elements: ");

            foreach(int num in lstarray)
            {
                Console.WriteLine(num);
            }



        }

        static List RemoveDuplicates(ref List arr)
        {

            int i = 0;

            //logic to remove the duplicate values.
            for (int j = i + 1; j < arr.Count; )
            {

                if (arr[i] == arr[j])
                {
                    arr.Remove(arr[i]);
                }
                else
                {
                    j++;
                }

            }




            return arr;
        }
    }
}

Related Posts