tags:

views:

102

answers:

1

If so, what is it?

EDIT: In response to comment below:

var tabulatedOutputErrors = from error in outputErrors
                            group error by error into errorGroup
                            select new { error = errorGroup.Key, number = errorGroup.Count() };
var tabulatedInputErrors = from error in inputErrors
                           group error by error into errorGroup
                           select new { error = errorGroup.Key, number = errorGroup.Count() };
var problems = tabulatedOutputErrors.Except(tabulatedInputErrors);

You can expand out the counts if you need to.

+8  A: 

LINQ has the Enumerable.Except extension method, which seems to be what you're looking for.

Example:

var list1 = new int[] {1, 3, 5, 7, 9};
var list2 = new int[] {1, 1, 5, 5, 5, 9};

var result = list1.Except(list2); // result = {3, 7}

Alternative:

From .NET 3.5 onwards there also exists the HashSet<T> class (and also the similar SortedSet<T> class in .NET 4.0. This class (or rather the ISet<T> interface in .NET 4.0) has an ExceptWith method which could also do the job.

Example:

var set1 = new HashSet<int>() {1, 3, 5, 7, 9};
var set2 = new HashSet<int>() {1, 1, 5, 5, 5, 9};

set1.ExceptWith(set2); // set1 = {3, 7}

Of course, it depends on the context/usage whether this approach is more desirable. The efficiency benefit (doing the difference operation in-place and using hash codes) in most cases is probably negligible. Either way, take your pick. :)

Noldorin
+1 -- however I am also curious if there's a way to do it without linq. If no such answer appears, this answer will be checkmarked.
Billy ONeal
@Billy ONeal: What's wrong with using Linq? Just curious if there's a clever non-declarative solution, or do you have some weird aversion to Linq?
Andy_Vulhop
@codemonkey4hire: No, it's just that Linq requires .NET 3.5.
Billy ONeal
+1 While this is the .NET equivalent, it's important to point out a few differences. `set_difference` is O(n), while `Except` is amortized O(n log n). This is because the sequences for `set_difference` must be ordered but `Except` does not require them to be ordered.
Stephen Cleary
A second difference is that `set_difference` will return multiple elements if they exist in the first set (and require multiple elements in the second set to "cancel" them); `Except` will always return a set containing unique elements (multiple elements in the first set are ignored, and can be "cancelled" by a single equal element in the second set).
Stephen Cleary
@Stephen: That is true. You can work around that by using group by statements.
Billy ONeal
@Billy: Could you give an example of this? I couldn't get it working without redefining `Except`.
Stephen Cleary
@Stephen Cleary: Good point regarding the time complexities. I do however believe that `SortedSet<T>` in .NET 4.0 should have an ExceptWith method that is simply O(n).
Noldorin
@Stephen: Well.. it's not a direct solution.. see edit in my question.
Billy ONeal
@Noldorin: I checked it out in Reflector, and it's definitely O(n log n). It has some special logic to avoid the `log n` part if the other element is outside the range of `this`, but it boils down to a `foreach` wrapping a call to `Remove`.
Stephen Cleary
@Billy: I think I wasn't clear. For an example, let `set1` be `{1,1,2,3}` and `set2` be `{1,2}`. `set_difference` would be `{1,3}` whereas `Except` would be `{3}`.
Stephen Cleary
@Stephen: What I have above would give you that. You'd just have to expand out the number manually after the linq query. I needed tabulation anyway so that's what I settled on.
Billy ONeal
@Billy: It's still not a full solution; try `{1,1,1,2,3}` and `{1,2}`, and you get `{1,3}` instead of the `set_difference` answer of `{1,1,3}`.
Stephen Cleary