Compare 2 arrays and list the differences - Swift

前端 未结 3 1296
再見小時候
再見小時候 2020-12-11 01:41

I was wondering how one would go about comparing 2 boolean arrays and listing the non matching booleans.

I have written up a simple example of 2 arrays.



        
3条回答
  •  时光说笑
    2020-12-11 02:40

    Here's one implementation, but whether it is the one you are after is completely impossible to say, because you have not specified what you think the answer should be:

    let answer = zip(array1, array2).map {$0.0 == $0.1}
    

    That gives you a list of Bool values, true if the answer matches the right answer, false if it does not.

    But let's say what you wanted was a list of the indexes of those answers that are correct. Then you could say:

    let answer = zip(array1, array2).enumerated().filter() {
        $1.0 == $1.1
    }.map{$0.0}
    

    If you want a list of the indexes of those answers that are not correct, just change == to !=.

提交回复
热议问题