0

I'm wondering what's the most efficient (edit: fastest) way of converting IEnumerable<HashSet<T>> to a single Hashset<T> with all its elements.

Sample situation:

Providing:

    class Rectangle
    {
        public HashSet<Point> PointSet { get; set; }
    }

Implement efficiently:

    HashSet<Point> ExtractUniquePoints(IEnumerable<Rectangle> rectangles) {}

Inefficient solution:

    HashSet<Point> ExtractUniquePoints(IEnumerable<Rectangle> rectangles)
    {
        HashSet<Point> uniquePoints = new HashSet<Point>();

        foreach (Rectangle rectangle in rectangles)
        {
            foreach (Point point in rectangle.PointSet)
            {
                uniquePoints.Add(point);
            }
        }

        return uniquePoints;
    }

Thanks in advance!

eduherminio
  • 1,514
  • 1
  • 15
  • 31
  • 2
    What do you mean by "most efficient"? Less code? Faster code? Less memory? Less GCs? – Enigmativity Dec 07 '18 at 02:11
  • 1
    I posted an answer, but I read your question again and now I'm confused so I removed my answer. Your question seems to be more complicated than simply combining an array of HashSets into a single HashSet, correct? You actually need some logic in here, right? – ProgrammingLlama Dec 07 '18 at 02:12
  • 2
    do you actually have a performance bottleneck? – Mitch Wheat Dec 07 '18 at 02:17
  • @Enigmativity I meant faster, edited. – eduherminio Dec 07 '18 at 02:19
  • @John was right, I've refactored the question since provided sample situation requires extra logic. – eduherminio Dec 07 '18 at 02:20
  • 2
    It is very unlikely to significantly improve code you have in the post as you already acting on each point once and using `foreach` instead of LINQ...If you believe there is particular piece of your code that does not perform well and have some reasoning for it to be faster - add that to your post. I think the real change would have to be level above for overall algorithm which should be separate question (maybe even of CS or SoftwareEngeneering SE sites) – Alexei Levenkov Dec 07 '18 at 03:44
  • `SelectMany()` was what I was trying to find, but you're right that the performance I was expecting requires facing my problem with a different approach. Thanks! – eduherminio Dec 07 '18 at 08:55

1 Answers1

2

I don't think there's necessarily a faster way to do what you're doing (at least not with HashSets), but a shorter method is the following:

HashSet<Point> ExtractUniquePoints(IEnumerable<Rectangle> rectangles)
{
    return new HashSet<Point>(rectangles.SelectMany(r => r.PointSet));
}

Or if you're using .NET Core, simply:

HashSet<Point> ExtractUniquePoints(IEnumerable<Rectangle> rectangles)
{
    return rectangles.SelectMany(r => r.PointSet).ToHashSet();
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86