Test whether two IEnumerable have the same values with the same frequencies

前端 未结 4 1115
轻奢々
轻奢々 2021-01-02 01:39

I have two multisets, both IEnumerables, and I want to compare them.

string[] names1 = { \"tom\", \"dick\", \"harry\" };
string[] names2 = { \

4条回答
  •  盖世英雄少女心
    2021-01-02 02:25

    @cdhowie's answer is great, but here's a nice trick that makes it even better for types that declare .Count by comparing that value prior to decomposing parameters to IEnumerable. Just add this to your code in addition to his solution:

        public static bool UnsortedSequencesEqual(this IReadOnlyList first, IReadOnlyList second, IEqualityComparer comparer = null)
        {
            if (first.Count != second.Count)
            {
                return false;
            }
    
            return UnsortedSequencesEqual((IEnumerable)first, (IEnumerable)second, comparer);
        }
    

提交回复
热议问题