2

I have two string arrays containing file names. From those two arrays, I need to produce a third string array that contains only the file names that are unique (i.e., found in one array, but not in both arrays).

2 Answers 2

6
var elements = array1.Union(array2).Except(array1.Intersect(array2));
Sign up to request clarification or add additional context in comments.

4 Comments

The coders on StackOverflow also never cease to amaze!
@grant probably because of the Except call. It isn't needed.
Read the spec, @StraxTillbaka basically wants XOR on an array, `A⊕B == (A AND B) NOT (A OR B), Lee's code does exactly that
@jdpenix true, I paid too nuch attention to the unique description, not the ie bit :-)
1

Consider using HashSet<T>. It has an implementation of exactly what you're looking for.

var setA = new HashSet<string>(arrayA);  
setA.SymmetricExceptWith(arrayB); // setA is mutated 

Obviously, you'll need to measure if the cost of initializing a HashSet is worth it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.