How do I check in Swift if two arrays contain the same elements regardless of the order in which those elements appear in?

前端 未结 8 1190
臣服心动
臣服心动 2020-12-02 22:20

Let\'s say there are two arrays...

var array1 = [\"a\", \"b\", \"c\"]
var array2 = [\"b\", \"c\", \"a\"]

I\'d like the result of the compar

8条回答
  •  渐次进展
    2020-12-02 22:47

    Swift 5.2 Solution

    var array1 = ["a", "b", "c"]
    var array2 = ["b", "c", "a"]
    
    if array1.sorted() == array2.sorted() {
        print("array 1 & array 2 are same")
    }
    

提交回复
热议问题