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

前端 未结 2 1176
自闭症患者
自闭症患者 2020-12-03 06:55

Assuming:

val l1 = List(1,2,3) 
val l2 = List(2,3,1)

I want a method that confirms that l1 is equal to l2 (as in same contents but differen

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 07:33

    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.

提交回复
热议问题