Comparing two collections for equality irrespective of the order of items in them

后端 未结 19 1914
我在风中等你
我在风中等你 2020-11-22 10:28

I would like to compare two collections (in C#), but I\'m not sure of the best way to implement this efficiently.

I\'ve read the other thread about Enumerable.Sequen

19条回答
  •  [愿得一人]
    2020-11-22 11:06

    EDIT: I realized as soon as I posed that this really only works for sets -- it will not properly deal with collections that have duplicate items. For example { 1, 1, 2 } and { 2, 2, 1 } will be considered equal from this algorithm's perspective. If your collections are sets (or their equality can be measured that way), however, I hope you find the below useful.

    The solution I use is:

    return c1.Count == c2.Count && c1.Intersect(c2).Count() == c1.Count;
    

    Linq does the dictionary thing under the covers, so this is also O(N). (Note, it's O(1) if the collections aren't the same size).

    I did a sanity check using the "SetEqual" method suggested by Daniel, the OrderBy/SequenceEquals method suggested by Igor, and my suggestion. The results are below, showing O(N*LogN) for Igor and O(N) for mine and Daniel's.

    I think the simplicity of the Linq intersect code makes it the preferable solution.

    __Test Latency(ms)__
    N, SetEquals, OrderBy, Intersect    
    1024, 0, 0, 0    
    2048, 0, 0, 0    
    4096, 31.2468, 0, 0    
    8192, 62.4936, 0, 0    
    16384, 156.234, 15.6234, 0    
    32768, 312.468, 15.6234, 46.8702    
    65536, 640.5594, 46.8702, 31.2468    
    131072, 1312.3656, 93.7404, 203.1042    
    262144, 3765.2394, 187.4808, 187.4808    
    524288, 5718.1644, 374.9616, 406.2084    
    1048576, 11420.7054, 734.2998, 718.6764    
    2097152, 35090.1564, 1515.4698, 1484.223
    

提交回复
热议问题