Skip to content

Remove everything which is duplicate in a List<List<double[]

HappyCode HappyCode

HappyCode HappyCode

Asked Jun 24, 2015, 7:43 AM EDT1 answer
Share
LinkedInX

Hi, I hope you can help me out on this one.

 
I have a List < List < double[] > > and I want to remove everything which is duplicate in such list. That is:

1) Within the List < double[] > there are some of the double[] which are duplicate.I want to keep only the non-duplicate doubles[] within the List < double[] >. 

2) Within List < List < double[] > > there are some of the List < double[] > which are duplicate. I want to keep only the non-repeated lists. 

 
I have tried the following but it doesn't work.
Java
public static List<List<double[]>> CleanListOfListsOfDoubleArray(List<List<double[]>> input)
{
          var output = new List<List<double[]>>();

          for (int i = 0; i < input.Count; i++)
          {
                    var temp= input[i].Distinct().ToList();
                    output.Add(temp);
          }
          return output.Distinct().ToList();
}
 
 
Any suggestions?
 
Many thanks!!! 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • HappyCode HappyCode
    0
    Problem solved!!
     
    Java
    public class ArrayComparer<T> : IEqualityComparer<T[]>
    {
        public bool Equals(T[] x, T[] y)
        {
            return ReferenceEquals(x, y) || (x != null && y != null && x.SequenceEqual(y));
        }
    
        public int GetHashCode(T[] obj)
        {
            return 0;
        }
    }
    
    public class ListOfArrayComparer<T> : IEqualityComparer<List<T[]>>
    {
        public bool Equals(List<T[]> x, List<T[]> y)
        {
            return ReferenceEquals(x, y) || (x != null && y != null && x.SequenceEqual(y, new ArrayComparer<T>()));
        }
    
        public int GetHashCode(List<T[]> obj)
        {
            return 0;
        }
    }
    The code should then look like this:
    Java
        public static List<List<double[]>> CleanListOfListsOfDoubleArray(List<List<double[]>> input)
        {
            var output = new List<List<double[]>>();
    
            for (int i = 0; i < input.Count; i++)
            {
                var temp = input[i].Distinct(new ArrayComparer<double>()).ToList();
                output.Add(temp);
            }
            return output.Distinct(new ListOfArrayComparer<double>()).ToList();
        }
    Or even just:
    Java
        public static List<List<double[]>> CleanListOfListsOfDoubleArray(List<List<double[]>> input)
        {
            var output = input.Select(t => t.Distinct(new ArrayComparer<double>()).ToList()).ToList();
    
            return output.Distinct(new ListOfArrayComparer<double>()).ToList();
        }
    0