Compare 2 arrays and specify the differences - Swift

I was wondering how one could compare 2 Boolean arrays and list inappropriate Boolean arrays.

I wrote a simple example of 2 arrays.

let array1 = [true, false, true, false] let array2 = [true, true, true, true] 

How would I compare array1 and array2 and display a mismatch. I am trying to do this to check the search results for a quiz.

Thanks!

+5
source share
1 answer

Here is one implementation, but whether it is the one you are behind is completely impossible to say, because you did not indicate that, in your opinion, the answer should be:

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

This gives you a list of Bool values, true if the answer matches the correct answer, false if it is not.

But tell me what you wanted, this is a list of 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 the index list of these answers to be incorrect, just change == to != .

+24
source

All Articles