C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Union works on Lists and arrays. We use integer arrays, but they could be string arrays or integer List types.
ListArrayThus: The Union of 1, 2, 3 and 2, 3, 4 is 1, 2, 3, 4. Elements are here compared for equality by Union using the default comparison logic.
C# program that invokes Union method
using System;
using System.Linq;
class Program
{
static void Main()
{
// Create two example arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Union the two arrays.
var result = array1.Union(array2);
// Enumerate the union.
foreach (int value in result)
{
Console.WriteLine(value);
}
}
}
Output
1
2
3
4
C# program that uses Union, unsorted chars
using System;
using System.Linq;
class Program
{
static void Main()
{
char[] values1 = { 'a', 'z', 'c' };
char[] values2 = { 'c', 's' };
// Take union.
var result = values1.Union(values2);
Console.WriteLine(string.Join(",", result));
}
}
Output
a,z,c,s