One way to combine two arrays without duplicatevalues in C# is to use LINQ and the
Union method.
Here's how you can do it:
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
// 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:
// 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 :
// 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.
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:
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
One way to combine two arrays without duplicate values in C# is to use LINQ and the
Unionmethod.Here's how you can do it:
In this example,
Unionis used to mergearray1andarray2. TheUnionthe method returns distinct elements from both arrays. Finally,ToArrayis 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
This function returns the unique values from any type of array.
2. Create a function to merge two arrays
Using HashSet:
Using List :
Let's merge the whole code into one program.
Output: