Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:03:09+00:00 2026-06-17T08:03:09+00:00

Suppose I have a class as follows… public class IntGroup { public string GroupName

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:03:10+00:00Added an answer on June 17, 2026 at 8:03 am

    Define this class first:

    class ValueIEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>>
    {
        public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
        {
            return x.SequenceEqual(y);
        }
    
        public int GetHashCode(IEnumerable<T> obj)
        {
            return obj.Sum(i => i.GetHashCode());
        }
    }
    

    Then, you can use this chain:

    int[][] groups =
    {
        new[] {1, 2, 3}, 
        new[] {4, 5, 6},
        new[] {4, 5, 9}
    };
    var result = groups.
        GroupBy(array => groups.
            Where(other => array != other).
            SelectMany(other => array.Intersect(other)), 
                new ValueIEnumerableComparer<int>()).
        Select(g => g.ToArray()).
        ToArray();
    

    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. Its GetHashCode() 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: class Foo { public String Bar { get; set; } }
Suppose I have very basic Task class as follows: public class Task { string
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Let's say I have a class as follows: public class AcceptMethods { public int
Suppose I have a class as follows: class Solution { public: std::vector<double> x; }
Suppose I have a diamond inheritance situation as follows: class A{ public: virtual void
Suppose I have a Base class and its derived class Derived as follows: class
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have: class Foo { ... }; class Bar : public Foo {
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.