Remove Duplicates from an Array in C# without using a built-in function

How to Remove Duplicates from an Array in C# without using a built-in function

To remove duplicates from an array without using built-in functions, create a custom function that iterates through the original array. This function will create a new array and insert elements based on a condition, if elements already exist it will be skipped, and if it’s not present it will have added to the new array.

In this article, we will learn How to Remove Duplicates From an Array in c# without using a built-in function.

Remove Duplicates From an Array in c# without using a built-in function.

# Method-1: Create a Function to Remove Duplicates

 public static IEnumerable<T> RemoveDuplicate<T>(this IEnumerable<T> values)
        {
            List<T> ArrayList = new List<T>();
            foreach (T value in values)
            {
                if (!ArrayList.Contains(value))
                {
                    ArrayList.Add(value);
                }
            }
            return (IEnumerable<T>) ArrayList;
        }

# Method-2: Create a Function to Remove Duplicates

  public static string[] removeduplicateElements(string[] elements)
        {
            List<string> newArray = new List<string>();
            foreach (string element in elements)
            {
                if (!newArray.Contains(element))
                {
                    newArray.Add(element);
                }
            }
           
            return (string[])newArray.ToArray();
        }

Complete Source


using System.Collections;
using System.Linq;
using System.Runtime.CompilerServices;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace CSharpTutorials
{
    public static class  Program
    {
        static void Main(string[] args)
        {
            int[] duplicatevalue = { 1, 2, 3, 4, 5, 6, 1, 3, 5, 5, 5, 5, 5, };

            IEnumerable arrayList = (IEnumerable)RemoveDuplicate<int>(duplicatevalue);

            foreach (var value in arrayList)
            {
                Console.WriteLine(value);
            }

            string[] stringduplicate = { "John", "Miller", "Raj", "Vicky", "John", "Raj" };

            string[] distinctArrayList = removeduplicateElements(stringduplicate);   //

            foreach (var value in distinctArrayList)
            {
                Console.WriteLine(value);
            }

             
        }

        public static IEnumerable<T> RemoveDuplicate<T>(this IEnumerable<T> values)
        {
            List<T> ArrayList = new List<T>();
            foreach (T value in values)
            {
                if (!ArrayList.Contains(value))
                {
                    ArrayList.Add(value);
                }
            }
            return (IEnumerable<T>) ArrayList;
        }

        public static string[] removeduplicateElements(string[] elements)
        {
            List<string> newArray = new List<string>();
            foreach (string element in elements)
            {
                if (!newArray.Contains(element))
                {
                    newArray.Add(element);
                }
            }
           
            return (string[])newArray.ToArray();
        }
    }
}
 

Here’s the Output:

1
2
3
4
5
6

John
Miller
Raj
Vicky

More articles – Call Stored Procedure in Web API without Entity Framework

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments