remove duplicates from array in c# using for loop Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Wed, 27 Sep 2023 12:20:55 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 remove duplicates from array in c# using for loop Archives - Tech Insights 32 32 230003556 How to Remove Duplicates from an Array in C# without using a built-in function https://reactconf.org/how-to-remove-duplicates-from-an-array-in-c-without-using-a-built-in-function/ https://reactconf.org/how-to-remove-duplicates-from-an-array-in-c-without-using-a-built-in-function/#respond Wed, 27 Sep 2023 12:20:55 +0000 https://labpys.com/?p=1893 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 …

The post How to Remove Duplicates from an Array in C# without using a built-in function appeared first on Tech Insights.

]]>
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

The post How to Remove Duplicates from an Array in C# without using a built-in function appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-remove-duplicates-from-an-array-in-c-without-using-a-built-in-function/feed/ 0 1893
How to Remove Duplicates from an Array in C# https://reactconf.org/csharp-remove-duplicates/ https://reactconf.org/csharp-remove-duplicates/#respond Mon, 15 May 2023 02:39:59 +0000 https://labpys.com/?p=1402 If you’ve ever worked with arrays in C#, you’ve probably encountered the issue of eliminating duplicate data. There is no built-in function in C# for removing duplicates from an array. …

The post How to Remove Duplicates from an Array in C# appeared first on Tech Insights.

]]>
If you’ve ever worked with arrays in C#, you’ve probably encountered the issue of eliminating duplicate data. There is no built-in function in C# for removing duplicates from an array. You can achieve the same objective, though, by creating a new array with different values. In this post, we will look at How to Remove Duplicates from an Array in c#.

Remove Duplicate Element in Array C#

As previously indicated, C# doesn’t have a built-in function for removing duplicate values from an array. We can instead build a new array with separate values. Getting unique values refers to the process of constructing a new array containing separate values.

The following example uses the Distinct() function to extract distinct values from an array and then builds a new array.

Remove Duplicate in an integer array

  public static int[] removeduplicatearray()
        {
            int[] arraynum = { 2, 5, 4, 3, 2, 7 };
            int[] distinctarray = arraynum.Distinct().ToArray();

            return distinctarray;
        }

We have an integer array arraynum with six members in the above code, and we want to delete the duplicates. We use the Distinct() function to create a new array that only contains the original array’s distinct members. Finally, we transform the output into an array and assign it to the original array.

Remove Duplicate in a string array

  public static string[] removeduplicatearraystring()
        {
            string[] arraystr = { "Apple", "Samsung", "Honor", "Redmi", "Apple" };
            string[] distinctarraystring = arraystr.Distinct().ToArray();

            return distinctarraystring;
        }

Try it

We have an integer array arraystr with five members in the above code, and we want to delete the duplicates. We use the Distinct() function to create a new array that only contains the original array’s distinct members. Finally, we transform the output into an array and assign it to the original array.

More Related Post: Remove Elements from an Array in C#

Conclusion

In this article, we explored How to Remove Duplicates from an Array in c#. We learned that we can achieve the same result by creating a new array with distinct values

The post How to Remove Duplicates from an Array in C# appeared first on Tech Insights.

]]>
https://reactconf.org/csharp-remove-duplicates/feed/ 0 1402