Which is faster: Union or Concat?

Jader Dias picture Jader Dias · Aug 26, 2009 · Viewed 15k times · Source

Which is faster: Union or Concat?

I don't care about the order of the elements.

Enumerable.Union Method

Enumerable.Concat Method

Answer

richardtallent picture richardtallent · Aug 26, 2009

Union removes duplicates. Concat does not.

So, they produce different results if the sources either contain any items in common, or have any internal duplicates.

If you can guarantee there are no duplicates, or if there are few and you don't care about having them in your output, Concat will be faster since there's no need to test each value against what has already been yielded.

However, if there are many duplicates and you don't need them, the extra processing in Union to remove the dupes may be offset by the savings in your code that consumes the results.