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!