alvaroLurtis
alvaroLurtis

Reputation: 19

How to distinct for a list of list using LINQ?

in my case, I have list of list of points I want to discard the lists of duplicate points using linq in a similar way to how the distinct works in List of items usually.

How could I do it? Here it is a code snippet to understand better my issue

var points = List<List<Point>>();

public struct Point: IEquatable<Point>
{

        public Point(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }


        public int X { get; }
        public int Y { get; }
}

Thanks

Upvotes: 0

Views: 276

Answers (4)

You can use HashSet this class provide high-performance set operations. A set is a Collection that CONTAINS NO DUPLICATE elements, and whose elements are in no particular order

Example:

    public struct Point 
    {
        public HashSet<int> coordinateX;
        public HashSet<int> coordinateY;

        public Point(HashSet<int> a, HashSet<int> b) 
        {
            coordinateX = a;
            coordinateY = b;
        }
    }
    static void Main(string[] args)
    {
        var set1 = new HashSet<int>() { 2, 3, 4, 6, 8 };
        var set2 = new HashSet<int>() { 67, 31, 1, 3, 5 };

        var points = new List<List<Point>>();

        points.Add(new List<Point>() { new Point(set1, set2) });

        //TODO
    }

Upvotes: 1

Rick the Scapegoat
Rick the Scapegoat

Reputation: 1086

Linq Distinct can use an IEqualityComparer<>

You can implement a new class implementing that interface and then generate distinct lists.

Here is an implementation of a fully coded Point implementing IEquatable<>, and implemented IEqualityComparer<> and a Test class.

The test class does a 'simple' distinct on each list. If you need an more complex distinct, such as distinct points across all lists, post your functional requirements and I can see what i can do.

    public struct Point : IEquatable<Point>
{
    public Point(int x, int y) : this()
    {
        X = x;
        Y = y;
    }

    public int X { get; set; }
    public int Y { get; set; }

    public bool Equals(Point other)
    {
        if (other.X == X && other.Y == Y)
            return true;

        return false;
    }

    public override bool Equals(object obj)
    {
        if (obj != null && obj.GetType() == typeof(Point))
            return Equals((Point)obj);

        return base.Equals(obj);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(X, Y);
    }

    public int GetHashCode(Point obj)
    {
        return obj.GetHashCode();
    }
}

public class PointComparer : IEqualityComparer<Point>
{
    public bool Equals(Point x, Point y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(Point obj)
    {
        return obj.GetHashCode();
    }
}

public class Tester
{
    public static List<List<Point>> Dist(List<List<Point>> points)
    {
        var results = new List<List<Point>>();
        var comparer = new PointComparer();

        foreach (var lst in points)
        {
            results.Add(lst.Distinct(comparer).ToList());
        }

        return results;
    }
}

Upvotes: -1

Hogan
Hogan

Reputation: 70523

If you have code like this

public class Point : IEquatable<Point>
{
  public Point(int x, int y)
  {
    this.X = x;
    this.Y = y;
  }

  public int X { get; }
  public int Y { get; }


  public bool Equals(Point other)
  {
    //Check whether the compared object is null. 
    if (Object.ReferenceEquals(other, null)) return false;

    //Check whether the compared object references the same data. 
    if (Object.ReferenceEquals(this, other)) return true;

    //Check whether the products' properties are equal. 
    return X.Equals(other.X) && Y.Equals(other.Y);
  }

  // If Equals() returns true for a pair of objects  
  // then GetHashCode() must return the same value for these objects. 

  public override int GetHashCode()
  {

    //Get hash code for the Name field if it is not null. 
    int hashProductX = X == null ? 0 : X.GetHashCode();

    //Get hash code for the Code field. 
    int hashProductY = Y == null ? 0 : Y.GetHashCode();

    //Calculate the hash code for the product. 
    return hashProductX ^ hashProductY;
  }
}

then this code would work

var distinct_points = points.Distinct();

assuming points is defined like this

List<Point> points;

you can also use

var distinct_points = points.SelectMany(x => x).Distinct();

if points is defined like this

var points = List<List<Point>>();

documentation this example was adapted from https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8

Upvotes: -1

pwilcox
pwilcox

Reputation: 5753

First, actually implement IEquatable:

public struct Point: IEquatable<Point> {

    public Point(int x, int y) {
        this.X = x;
        this.Y = y;
    }

    public int X { get; }
    public int Y { get; }

    public bool Equals (Point other) => 
        this.X == other.X && this.Y == other.Y;

}

Then create a custom equality comparer. That requires an equality logic, and a hash code generator. For the equality logic, use SequenceEqual, for the hash code generator, you'll have to play around with it, but here's an example via Jon Skeet. I used part of his logic below:

class ListPointComparer : IEqualityComparer<List<Point>> {

    public bool Equals(List<Point> a, List<Point> b) => a.SequenceEqual(b);

    public int GetHashCode(List<Point> list) {
        int hash = 19;
        foreach(var point in list)
            hash = hash * 31 + point.GetHashCode();
        return hash;
    }

}

Now imagine points like this:

var pointsA = new List<Point> { new Point (1,1), new Point(2,2) };
var pointsB = new List<Point> { new Point (1,1), new Point(2,2) };
var pointsC = new List<Point> { new Point (3,3), new Point(4,4) };
var pointLists = new List<List<Point>> { pointsA, pointsB, pointsC };   

Use your comparer class:

var results = pointLists.Distinct(new ListPointComparer());
// Outputs only 2 lists, with pointsA and pointsB combined.

Upvotes: 2

Related Questions