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

前端 未结 4 1113
轻奢々
轻奢々 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:28

    You could use a binary search tree to ensure that the data is sorted. That would make it an O(log N) operation. Then you can run through each tree one item at a time and break as soon as you find a not equal to condition. This would also give you the added benefit of being able to first compare the size of the two trees since duplicates would be filtered out. I'm assuming these are treated as sets, whereby {"harry", "harry"} == {"harry").

    If you are counting duplicates, then do a quicksort or a mergesort first, that would then make your comparison operation an O(N) operation. You could of course compare the size first, as two enums cannot be equal if the sizes are different. Since the data is sorted, the first non-equal condition you encounter would render the entire operation as "not-equal".

提交回复
热议问题