Suppose I have a class as follows…
public class IntGroup {
public string GroupName {get; set;}
public List<int> Integers {get; set;}
}
…and I have several instances, each containing a collection of integers. I want to find the smallest sets of groups that contain distinct integers.
For example, if I have the following groups…
Group 1 contains 1, 2, 3
Group 2 contains 4, 5, 6
Group 3 contains 4, 5, 9
…then as group 1 contains three integers that aren’t in any other group, it on its own is a smallest set of groups (in this case, a set of one). Groups 2 and 3 together are another smallest set, in that you need both groups to be together (as they both contain 4 and 5), but they don’t need group 1.
I would like to write some C# code that would help me find these smallest groups. It’s the sort of problem that I feel could be very elegantly solved in Linq, but I can’t work out how.
Anyone able to help? By the way, this isn’t a homework question, I’m a 51 year-old programmer, looking to solve part of a much bigger problem to do with building trees of function calls, and wanting to find distinct parts of the tree.
Thanks for any help you can give.
Define this class first:
Then, you can use this chain:
What it does is group the arrays by their intersections, then select only the array from the group. Defining a class implementing
IEqualityComparer<IEnumerable<T>>was necessary. I couldn’t find a better way to extract a key on two sequences without defining a comparer which would compare their elements. ItsGetHashCode()method isn’t really fancy but works for this example.It can also be easily adapted to your case, but I thought that bringing a general one would be more helpful for whoever reads the question.