我需要比较两组 A 和 B 。我需要为
进行处理我考虑过的方法:
我可以使用算法described here(并粘贴在下面),并在线进行处理。这很好,因为它只遍历每个集合中的元素一次。
Given input sets A,B, each a strictly ascending list. Initialize A\B and B\A as empty lists. Until A or B is empty, compare the heads of both lists If the heads are equal, remove them. If the head of A preceeds the head of B, remove the head from A and include it in A\B. If the head of B preceeds the head of A, remove the head from B and include it in B\A. Now A or B is empty. Transfer entries left in A to A\B or entries left in B to B\A.
我可以使用Hashset<T>
(Intersect
,ExceptWith
)或Enumerable
Linq扩展方法(Intersect
,{{}上的方法执行此操作3}})。它们的优点是使我的代码/意图更具可读性,但我最终会多次迭代原始集。
问题: 有没有一种方法,我可以获得选项#2的可读性与#1的性能?