0

How do would you get all the distinct int[] arrays in a list?

Example:

List<int[]> list = new List<int[]>() {
  new int[] { 1, 2, 3, 4 },
  new int[] { 1, 2, 3, 4 },
  new int[] { 2, 3, 4, 4 },
};

I want to return distinct arrays from list assuming the array is sorted.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
newuser
  • 9
  • 1
  • Well, first you would need a way of comparing if one array is distinct from another array. Do you know how to do that? – gunr2171 May 16 '22 at 18:08
  • I know that there is a list.Distinct() method in C# but I don't know if it applies to arrays as well. Would this just have to be done manually? – newuser May 16 '22 at 18:10
  • @newuser no, `Distinct` will not work on arrays, but it is easy to check. – Guru Stron May 16 '22 at 18:11
  • Thanks. Just wanted to see if there was a faster way to do so. – newuser May 16 '22 at 18:16
  • Write a method that accepts two arrays as arguments, and returns a bool for whether they are distinct or not. – Joel Coehoorn May 16 '22 at 18:17
  • @RyanWilson Checking sum and length won't work. {2, 3} and {1, 4} will be equal in this logic. But they're different. I guess it's required to write a custom compare method for comparing 2 arrays and storing them accordingly. – Md. Faisal Habib May 16 '22 at 18:28
  • 1
    This looks like a duplicate of: [delete-duplicates-in-a-list-of-int-arrays](https://stackoverflow.com/questions/37850167/delete-duplicates-in-a-list-of-int-arrays) – Ryan Wilson May 16 '22 at 18:34
  • @Jasen I think the comment specifies that each array will be sorted: `// return distinct arrays from list assuming the array is sorted` – Ryan Wilson May 16 '22 at 18:37
  • @RyanWilson +1, my habit of ignoring code comments ;) – Jasen May 16 '22 at 18:38
  • @Jasen No worries. :P – Ryan Wilson May 16 '22 at 18:39
  • Does this answer your question? [Delete duplicates in a List of int arrays](https://stackoverflow.com/questions/37850167/delete-duplicates-in-a-list-of-int-arrays) – Pranav Hosangadi May 16 '22 at 19:22

1 Answers1

0

When working with distinct we should come to terms how to compare items (by default .net compares references, and that's why new[] {1, 2, 3} != new int[] {1, 2, 3} since arrays' don't share the same reference).

We can do it while implementing IEqualityComparer<T> for this, e.g.

public sealed class EnumerableEqualityComparer<T> 
  : IEqualityComparer<IEnumerable<T>> {
  
  public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y) {
    if (ReferenceEquals(x, y))
      return true;
    if (ReferenceEquals(null, x) || ReferenceEquals(null, y))
      return false;

    return x.SequenceEqual(y);
  }

  public int GetHashCode(IEnumerable<T> obj) {
    if (obj is null)
      return -1;

    if (obj is ICollection<T> c)
      return c.Count;

    // Count() without restriction - Take(10) - can be expensive
    return obj.Take(10).Count();
  }
}

and then use it within Distinct:

var result = list
  .Distinct(new EnumerableEqualityComparer<int>())
  .ToList(); // let's have a list of the distinct arrays
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215