How to Combine Two Arrays without Duplicate values in C#?In this article, we will discuss how to combine two arrays without duplicate values in C#. Let us assume there are two arrays, and the job is to combine or merge these arrays into one array that must not have duplicate values. This way operates with two arrays, removing the duplicates in both of them. If two elements are in an array, it is supposed to take the element only one time. Array merging in C# implies a combination of two arrays into one array. The merge of two arrays may cause the duplicates to appear in the resulting array. Let us take a look to better understand this thing via its syntax and examples. Syntax:It has the following syntax: Example:Input1: Input2: 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. Now, perform the transformation for the elements in the last array using the ForEach() function. 4. Another advantages of the Array is enabling us to iterate the array with the IEnumerable method. Example 1:Let us take a C# program to merge two arrays into a single array without duplicate elements. 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 example, two number arrays, arr1 and arr2, are defined and then initialized with some values. These arrays stand for the merger arrays. The element of the array arr1 is printed, which is performed in foreach loop using Console.WriteLine method. Also, the components of arr2 are printed by using another foreach loop and the Console.WriteLine method. The union() method is called upon arr1 and arr2 to unify the unique elements of both arrays. The Union method is a type of operator that produces single distinct elements from various supplies. At this stage, the combined array is converted to an array using the ToArray method. In the end, the components of the finalArray are printed by the Array.ForEach method and the Console.WriteLine method. Example 2:Let us take another C# program to merge two arrays into a single array without duplicate elements. 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 example, the two strings, arr1, and arr2, are declared and then initialized with some values. They are the ones to represent the two arrays that have to be merged. After that, we get the element's of arr1 printed with the foreach loop and Console.WriteLine. Just like that, the other foreach loop and Console.WriteLine method are used for printing the elements of the arr2. The Union method is applied to arr1 and arr2, which results in an array with the unique elements from the two arrays. Considering that we work with string arrays, the resulting array contains pure string values. The finalarray has type IEnumerable<string> to contain the unique elements. Lastly, a foreach loop and the Console.WriteLine method is used to print the elements of the finalArray. Conclusion:In summary, the given C# code illustrates how to put two arrays into a single array without repeating elements. It is designed and implemented for integer arrays and string arrays. Considering integers to be the elements, the program uses the Union method from the LINQ library to get the unique elements from both arrays. The resulting table contains all the distinct elements of the original arrays. Also, the program follows the same pattern for string arrays by using the Union method to join the arrays and, in the output, get a new array with unique string values. The algorithm utilizes the Union method to convert the result to an array, eliminating duplicated elements and creating a new array with unique elements from the original arrays. Next TopicHow to compare Enum values in C#? |