How To Combine Two Arrays Without Duplicate Values In C#

In this tutorial, we will explore the process of merging two arrays in C# while ensuring no duplicate values are present in the resulting array. Consider two arrays that need to be merged without any duplicate elements. The merging process involves combining the arrays and eliminating any duplicate values from both. Each element should appear only once in the final array to prevent redundancy.

Combining arrays in C# involves merging two arrays to create a single array. Merging arrays can lead to duplicate elements in the merged array. To gain a clearer understanding of this concept, we will explore the syntax and examples.

Syntax:

It has the following syntax:

Example

first_array.Union(second_array)

Example:

Input1:

Example

array1={1,2,3,4,5}
array2={3,8,9}
output={1,2,3,4,5,8,9}

Input2:

Example

array1={11,23,45,20}
array2={29,89,11,20}
output={11,20,23,29,45,89}

Approach:

  1. Write a program to declare two arrays (of any type int, string , etc).
  2. Utilize the Union function and cast as array using ToArray function.
  3. Example
    
    final = array1.Union(array2).ToArray();
    
  4. Next, apply the conversion to the items within the final array by utilizing the ForEach method.
  5. Example
    
    Array.ForEach(final, i => Console.WriteLine(i));
    
  6. Another benefit of using an Array is its capability to iterate through the array using the IEnumerable method.
  7. Example
    
    IEnumerable<int> final = array1.Union(array2);
    foreach (var in final)
    {
    Console.WriteLine(i);
    }
    

    Example 1:

Let's consider a C# code snippet to combine two arrays into one array while removing any duplicate elements.

Example

using System; 
using System.Collections.Generic; 
using System.Linq; 
//Program to implement how to combine two arrays without duplicate values 
class CombineArray
{ 
    public static void Main() 
    {
        // the initial array with integer values
        int[] arr1 = { 22, 33, 25, 34, 50, 32}; 
        //the second array with integer values
        int[] arr2 = { 21, 33, 21, 34, 29 }; 
        // The Array 1 
        Console.WriteLine("The 1st Array: "); 
        foreach (int a1 in arr1) 
        {
            Console.WriteLine(a1); 
        }
        // the console statement to print the array2
        Console.WriteLine("The 2nd Array: ");
        foreach (int a2 in arr2)
        { 
            Console.WriteLine(a2);
        }
        // the Union() method to combine unique elements 
        var finalArray = arr1.Union(arr2).ToArray();
        // Display the elements in the final array 
        Console.WriteLine("The new Array"); 
        Array.ForEach(finalArray, i => Console.WriteLine(i)); 
        
    }
}

Output:

Output

The 1st Array: 
22
33
25
34
50
32
The 2nd Array: 
21
33
21
34
29
The new Array
22
33
25
34
50
32
21
29

Explanation:

In this illustration, we have two numeric arrays, arr1 and arr2, which are declared and initialized with specific values. These arrays represent the combined arrays. The content of arr1 is displayed using a foreach loop along with the Console.WriteLine method. Similarly, the elements of arr2 are displayed using another foreach loop and the Console.WriteLine method. The union function is invoked on arr1 and arr2 to merge the distinct elements of both arrays.

The Union operation is an operator type that generates unique elements from multiple collections. After this step, the merged array is transformed into a list using the ToArray function. Finally, the elements of the finalArray are displayed using the Array.ForEach function and the Console.WriteLine function.

Example 2:

Let's explore a different C# code example that combines two arrays into one array without any duplicate elements.

Example

using System; 
using System.Collections.Generic; 
using System.Linq; 
//Program to implement how to combine two arrays without duplicate values 
class CombineArray
{ 
    public static void Main() 
   { 
    //the initial array with string values 
    string[] arr1 = { "Ramesh", "Ganesh", "Bob" };
    // the second array with the string datatype values
    string[] arr2 = { "RohithKumar", "Ramesh", "Bob" }; 
    Console.WriteLine("The 1st Array: ");
    foreach (string a1 in arr1) 
    {
        Console.WriteLine(a1);
    }
    // the console statement to print the array2 
    Console.WriteLine("The 2nd Array: ");
    foreach (string a2 in arr2)
    {
        Console.WriteLine(a2);
    }
    // the Union() method to combine unique elements 
    IEnumerable<string> finalArray = arr1.Union(arr2);
    // the console statement to display array elements 
    Console.WriteLine("The final array with unique elements is :"); ??
    foreach (var j in finalArray)
    {
        Console.WriteLine(j);
    }
}
}

Output:

Output

The 1st Array: 
Ramesh
Ganesh
Bob
The 2nd Array: 
RohithKumar
Ramesh
Bob
The final array with unique elements is :
Ramesh
Ganesh
Bob
RohithKumar

Explanation:

In this instance, the pair of strings, arr1 and arr2, are defined and subsequently populated with specific values. These variables serve as the representations of the two arrays intended for merging. Following this, the items within arr1 are displayed using a foreach loop in conjunction with Console.WriteLine. Similarly, the elements of arr2 are printed using another foreach loop paired with the Console.WriteLine method.

The Union operation is executed on arr1 and arr2, producing an array that includes only distinct elements from both arrays. In the case of string arrays, the resultant array exclusively consists of string values. The final array is of type IEnumerable<string> to store these unique elements. To display the elements of finalArray, a foreach loop combined with the Console.WriteLine method is employed.

Conclusion:

In essence, the provided C# script demonstrates the amalgamation of two arrays into a single array while ensuring no duplicate elements are present. It has been specifically crafted to cater to both integer arrays and string arrays. By treating integers as the primary elements, the script leverages the Union method available in the LINQ library to extract the distinct elements from each array. The resultant array encapsulates all the unique elements derived from the original arrays. Similarly, the script seamlessly extends this methodology to string arrays, employing the Union method to merge the arrays and generate a fresh array containing exclusive string values. Through the incorporation of the Union method, the algorithm effectively transforms the outcome into an array, effectively removing any redundancies and producing a novel array comprising unique elements sourced from the initial arrays.

Input Required

This code uses input(). Please provide values below: