---
title: "How to combine two arrays without duplicate values in C#?"  
description: "How to combine two arrays without duplicate values in C#?"  
author: "Steilla Mitchel"  
published: 2024-06-11  
updated: 2024-06-11  
canonical: https://www.mindstick.com/forum/160711/how-to-combine-two-arrays-without-duplicate-values-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# How to combine two arrays without duplicate values in C#?

How to [combine](https://www.mindstick.com/interview/1043/how-can-i-combined-two-variable-together) [two arrays](https://www.mindstick.com/forum/161074/how-to-compare-two-arrays-to-each-other-in-javascript) without [duplicate values](https://www.mindstick.com/forum/157764/how-to-find-duplicate-values-in-a-sql-table) in C#?

## Replies

### Reply by Ravi Vishwakarma

One way to combine two [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations) without [duplicate](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server) [values](https://www.mindstick.com/forum/327/sum-textbox-values) in C# is to use LINQ and the `Union` method.

Here's how you can do it:

```cs
using System;
using System.Linq;
class Program
{
    static void Main()
    {
        int[] array1 = { 1, 2, 3, 4 };
        int[] array2 = { 3, 4, 5, 6 };
        int[] combinedArray = array1.Union(array2).ToArray();
        foreach (var item in combinedArray)
        {
            Console.WriteLine(item);
        }
    }
}
```

In this example, `Union` is used to merge `array1` and `array2`. The `Union` the method returns distinct elements from both arrays. Finally, `ToArray` is used to convert the result into an array.

**Let us create a custom distinct and merge array function with two types.**

## 1. Create a Custom Generic Distinct function

```cs
        // Generic Method to return distinct elements from any array
        public static T[] DistinctArray<T>(T[] array)
        {
            IList<T> list = new List<T>(); // Create a list to store distinct elements

            foreach (var item in array)
            {
                // Add item to list if it's not already present
                if (!list.Contains<T>(item))
                {
                    list.Add(item);
                }
            }

            return list.ToArray<T>(); // Convert list to array and return
        }
```

This function returns the unique values from any type of array.

## 2. Create a function to merge two arrays

## Using HashSet:

```cs
        // Merge arrays using HashSet
        public static T[] MergeArrayWithHashSet<T>(T[] FirstArray, T[] SecondArray)
        {
            HashSet<T> mergedSet = new HashSet<T>(); // Create a HashSet to store merged elements
            foreach (var item in FirstArray)
            {
                mergedSet.Add(item); // Add elements of the first array to HashSet
            }
            foreach (var item in SecondArray)
            {
                mergedSet.Add(item); // Add elements of the second array to HashSet
            }
            T[] mergedArray = new T[mergedSet.Count]; // Create an array to store merged elements
            mergedSet.CopyTo(mergedArray); // Copy elements from HashSet to array
            return mergedArray; // Return merged array
        }
```

## Using List :

```cs
        // Merge arrays using List
        public static T[] MergeArrayWithList<T>(T[] FirstArray, T[] SecondArray)
        {
            IList<T> data = new List<T>(); // Create a list to store merged elements
            foreach (var item in FirstArray)
            {
                data.Add(item); // Add elements of the first array to list
            }
            foreach (var item in SecondArray)
            {
                data.Add(item); // Add elements of the second array to list
            }
            return DistinctArray<T>(data.ToArray()); // Return array with distinct elements
        }
```

Let's merge the whole code into one program.

```cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Original arrays
            int[] FirstArray = { 1, 2, 3, 1, 2, 3, 1, 4, 12 };
            int[] SecondArray = { 1, 22, 3, 1, 25, 3, 10, 4, 102 };

            Console.WriteLine("Merge Array With HashSet");
            // Merge arrays using HashSet
            int[] MergeArrayWithHashSet = Program.MergeArrayWithHashSet<int>(FirstArray, SecondArray);

            foreach (var item in MergeArrayWithHashSet)
            {
                Console.Write(item + ", "); // Print each element of the merged array
            }

            Console.WriteLine("\n\nMerge Array With List");
            // Merge arrays using List
            int[] MergeArrayWithList = Program.MergeArrayWithList<int>(FirstArray, SecondArray);
            Console.Write(string.Join(", ", MergeArrayWithList)); // Print each element of the merged array

            Console.ReadLine();
        }

        // Generic Method to return distinct elements from any array
        public static T[] DistinctArray<T>(T[] array)
        {
            IList<T> list = new List<T>(); // Create a list to store distinct elements

            foreach (var item in array)
            {
                // Add item to list if it's not already present
                if (!list.Contains<T>(item))
                {
                    list.Add(item);
                }
            }

            return list.ToArray<T>(); // Convert list to array and return
        }

        // Merge arrays using HashSet
        public static T[] MergeArrayWithHashSet<T>(T[] FirstArray, T[] SecondArray)
        {
            HashSet<T> mergedSet = new HashSet<T>(); // Create a HashSet to store merged elements

            foreach (var item in FirstArray)
            {
                mergedSet.Add(item); // Add elements of the first array to HashSet
            }
            foreach (var item in SecondArray)
            {
                mergedSet.Add(item); // Add elements of the second array to HashSet
            }

            T[] mergedArray = new T[mergedSet.Count]; // Create an array to store merged elements
            mergedSet.CopyTo(mergedArray); // Copy elements from HashSet to array

            return mergedArray; // Return merged array
        }

        // Merge arrays using List
        public static T[] MergeArrayWithList<T>(T[] FirstArray, T[] SecondArray)
        {
            IList<T> data = new List<T>(); // Create a list to store merged elements

            foreach (var item in FirstArray)
            {
                data.Add(item); // Add elements of the first array to list
            }
            foreach (var item in SecondArray)
            {
                data.Add(item); // Add elements of the second array to list
            }

            return DistinctArray<T>(data.ToArray()); // Return array with distinct elements
        }
    }
}
```

## Output:

![How to combine two arrays without duplicate values in C#?](https://www.mindstick.com/mindstickforums/6557aef8-fb48-4335-9fc3-32fc69afe304/images/e9b60267-c18a-4a30-a248-7528b6497f93.png)


---

Original Source: https://www.mindstick.com/forum/160711/how-to-combine-two-arrays-without-duplicate-values-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
