MoreLINQ icon indicating copy to clipboard operation
MoreLINQ copied to clipboard

Add AreCollectionsSame

Open Lonli-Lokli opened this issue 3 years ago • 3 comments

It's pretty useful and widely used feature, to check if two unordered collections are the same, probably with default and nondefault comparers

Lonli-Lokli avatar Apr 12 '21 12:04 Lonli-Lokli

you can easily create an extension method that does something like

var seq1 = list1.OrderBy(t => t);
var seq2 = list2.OrderBy(t => t);
return seq1.SequenceEqual(seq2);

or maybe

var set1 = list1.ToHashSet();
var set2 = lits2.ToHashSet();
return set1.SetEquals(set2);

leandromoh avatar May 22 '21 17:05 leandromoh

@leandromoh I understand that I can, I actually created test for myself. I was just hoping to see the most efficient way in this library available for everyone.

|           Method |       Size |           Mean |        Error |       StdDev |         Median |
|----------------- |----------- |---------------:|-------------:|-------------:|---------------:|
|  SortThenCompare |    Size 10 |       766.6 ns |     22.31 ns |     63.28 ns |       748.9 ns |
| ConvertToHashset |    Size 10 |       624.6 ns |     10.01 ns |      9.36 ns |       623.3 ns |
| OneExceptAnother |    Size 10 |       871.5 ns |      9.11 ns |      8.52 ns |       872.0 ns |
|  SortThenCompare |   Size 100 |    10,697.1 ns |    188.36 ns |    334.82 ns |    10,578.2 ns |
| ConvertToHashset |   Size 100 |     3,730.6 ns |     74.27 ns |    151.71 ns |     3,661.1 ns |
| OneExceptAnother |   Size 100 |     6,319.3 ns |    115.18 ns |    102.10 ns |     6,320.1 ns |
|  SortThenCompare |  Size 1000 |   179,022.0 ns |  2,306.86 ns |  2,157.84 ns |   178,539.6 ns |
| ConvertToHashset |  Size 1000 |    36,351.3 ns |    721.78 ns |  1,951.38 ns |    35,832.8 ns |
| OneExceptAnother |  Size 1000 |    58,811.9 ns |    843.54 ns |    789.05 ns |    59,027.8 ns |
|  SortThenCompare | Size 10000 | 2,428,770.1 ns | 47,772.12 ns | 81,120.69 ns | 2,397,744.7 ns |
| ConvertToHashset | Size 10000 |   345,496.7 ns |  6,631.58 ns | 10,518.39 ns |   344,402.7 ns |
| OneExceptAnother | Size 10000 |   598,397.5 ns | 11,831.15 ns | 16,967.88 ns |   599,364.8 ns |
        [Benchmark]
        [ArgumentsSource(nameof(Datasource))]
        public bool SortThenCompare(TestSource source)
        {
            var x = source.XValues;
            var y = source.YValues;

            return x.OrderBy(t => t).SequenceEqual(y.OrderBy(l => l));
        }
        
        [Benchmark]
        [ArgumentsSource(nameof(Datasource))]
        public bool ConvertToHashset(TestSource source)
        {
            var x = source.XValues;
            var y = source.YValues;

            return x.ToHashSet().SetEquals(y.ToHashSet());
        }
        
        [Benchmark]
        [ArgumentsSource(nameof(Datasource))]
        public bool OneExceptAnother(TestSource source)
        {
            var x = source.XValues;
            var y = source.YValues;

            return !x.Except(y).Any() && !y.Except(x).Any();
        }

Lonli-Lokli avatar May 22 '21 23:05 Lonli-Lokli

@Lonli-Lokli This has been implemented in SuperLinq, and will be released with v4.1.0 in the next few weeks.

viceroypenguin avatar Jul 27 '22 21:07 viceroypenguin