0

I have two arrays of strings. for example:

let arrayFirst: [String] = ["A", "A", "A", "A", "A"]
let arraySecond: [String] = ["A", "C", "A", "B", "A"]

I need to compare this two arrays each element in array and return for every sequence bool state. For example here will be answer:

 let resultArray: [Bool] = [true, false, true, false, true]

how to do it better?

1
  • What should the result be if the arrays are of different length? Maybe you could add your own attempt at solving this to clarify what you want to do and also since this is expected when posting a question. Commented Mar 22, 2022 at 10:13

1 Answer 1

3

You can consider using the zip function.

let resultArray = zip(arrayFirst, arraySecond).map {
    return $0.0 == $0.1
}

This will work even you have arrays of different length as zip will ignore the additional elements of the longer array.

Sign up to request clarification or add additional context in comments.

2 Comments

Even .map(==) should work.
Yes, but i need to handle false values ​​and true values..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.