Join Two String Arrays with Distinct values using LINQ

One of my colleagues was looking out for a simple way to join two string arrays and avoid duplicates (if any) in those arrays. I asked him to use the Union method which excludes duplicates from the return set.


static void Main(string[] args)
string[] arr1 = { "One", "Two", "Four", "Six" };
string[] arr2 = { "Three", "Two", "Six", "Five" };

var arr3 = arr1.Union(arr2);

foreach (string n in arr3)


VB.NET (option infer on)

Sub Main(ByVal args() As String)
Dim arr1() As String = { "One", "Two", "Four", "Six" }
Dim arr2() As String = { "Three", "Two", "Six", "Five" }

Dim arr3 = arr1.Union(arr2)

For Each n As String In arr3
Next n

End Sub


Merge String Arrays LINQ

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

Sachin Sankeshwari said...

We can also remove duplicates from the same array using union.

See for more details.

Thanks an regards,