0

I feel like this may call for reduce, map or something like it to solve but I'm not yet familiar enough with these and was hoping someone here might be. Lets say I have

arrayOne = [1, 3, 7] arrayTwo = [2, 1, 10]

the expected result for what I'm trying to do would be

mergedArray = [2, 3, 10]

I know I can do this with a relatively simple for loop in a method but I am looking for a more "swift" way to do it if it's possible.

And Yes, both arrays will always be the same length.

0

1 Answer 1

6

This will work:

let arrayOne = [1, 3, 7]
let arrayTwo = [2, 1, 10]

let mergedArray = zip(arrayOne, arrayTwo).map{max($0, $1)}

First, pair each element in two arrays with zip, and then use map to each pair.

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

1 Comment

Even shorter: zip(arrayOne, arrayTwo).map(max)

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.