Honestly, that method shouldn't be any faster than the original, since it's essentially doing the same thing behind the scenes.
(Edit: It's not even correct in the case where arr1
contains duplicate items that are also in arr2
! It's also not always faster: I tried it on short arrays and it was, but tried it on longer arrays and it wasn't.)
A parallel for Union
might be
HashSet<string> arr2Set = new HashSet<string>(arr2);
int unionSize = arr2Set.Count + arr1.Where(x=>arr2Set.Add(x)).Count();
but it really should be no different from doing
int unionSize = arr1.Union(arr2).Count();
If you're worried about performance, read this first. By trying this version and timing the two, you could well end up with the original version being faster - I certainly did.