There are two HashSet sequences. I need to count the result of union. It should look like this:
sequence1.UnionWith(sequence2);
return sequence1.Count;
But I need to ensure that the original sequences do not change. I used a simple search, but it very slows down the program
var newHashkSet = new HashSet<string>(sequence1);
foreach (var s2 in sequence2.Where(s2 => newHashkSet.SingleOrDefault(x => x == s2) == null)) {
newHashkSet.Add(s2);
}
return newHashkSet.Count();
Is there a more elegant solution?