Is there an API method that compares contents of a Seq irrespective of order?

混江龙づ霸主 提交于 2019-11-27 21:11:06

If what you want is "these lists contain the same elements, irrespective of order or repetitions":

l1.toSet == l2.toSet

If what you want is "these lists contain the same elements, and with the same number of repetitions of each":

l1.sorted == l2.sorted

If what you want is "these lists contain the same elements and are the same size, but the number of repetitions of a given element can differ between the two lists":

l1.size == l2.size && l1.toSet == l2.toSet

While

l1.sorted == l2.sorted

is correct, it's runtime performance is O(n log n), because of the sorting. For large lists, you are probably better with

l1.groupBy(identity) == l2.groupBy(identity)

which should be O(n), assuming a decent implementation of groupBy.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!