views:

89

answers:

2

Hi,

I have a string array Array1 and a string array A2. I want to combine these in a 3rd array A3 but excluding duplicate values. Can this be done through lambda expressions or only through iterating through the array and checking array.Contains()?

Thanks.

A: 

You can do the following

Dim newArray = array1.Union(array2).Distinct().ToArray()

This requires Visual Studio 2008 or higher (VB.Net 9.0).

JaredPar
You will additionally need a reference (not sure if it's termed the same in VB, I use C#) to Linq.
BarrettJ
@BarretJ, in 2008 it is added as a project level Import so it should already be there
JaredPar
Aren't there only 2 overloads to Distinct()? No params and 1 that takes IEqualityComparer
Keith Rousseau
@Keith, yes you're correct. Updated my answer
JaredPar
Thanks it works. I believe Distinct is unnecessary here as Union already returns the unique values.
MC.
+1  A: 
array1.Union(array2).Distinct().ToArray();
Keith Rousseau