10

I have the following two arrays:

let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]

I would like to merge them into an array that looks like this:

[ ("monday", 1), ("tuesday", 2), ("wednesday", 3), ("thursday", 4), ("friday", 5)]
2
  • This is not a dictionary that you are showing , its an array of tuples Commented Aug 28, 2017 at 3:02
  • updated question Commented Aug 28, 2017 at 3:03

4 Answers 4

24

Use zip and map:

let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]

let tuples = Array(zip(xaxis, yaxis))  // or `zip(xaxis, yaxis).map { ($0, $1) }`

I know you asked about a tuple, but you can also consider a custom object, e.g.:

struct Day {
    let name: String
    let value: Int
}

let names = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let values = [1, 2, 3, 4, 5]

let days = zip(names, values).map(Day.init)
Sign up to request clarification or add additional context in comments.

5 Comments

These methods are faster as compared to simple for loops or not, please give your suggestion
The above is my suggestion. Avoid premature optimization. Always use the most concise, most expressive syntax, and don't worry about optimization unless it can possibly have material impact. The performance difference is indistinguishable with 5 items. And it's practically nonexistent when doing release builds. All of that having been said, I wonder why you're using tuples at all. Or why are you using an array of string literals as opposed to Calendar.current.weekdaySymbols. There are many things I'd worry about before spending a second thinking about replacing zip/map with for loops.
I used those two arrays as an example but the real app won't have months or any calendar data in the array. I just wanted an example of how to convert two arrays, one of type string and one of type int, into an array of tuples.
.map { ($0, $1) } doesn't transform, so Array(zip(xaxis, yaxis)) is better, de-conflating the transform+Array.init that map normally entails.
Agreed. Array(zip(…)) is probably better. Thanks.
3

Try this:

let arrayMerged = zip(xaxis, yaxis).map { ($0, $1) }

or this:

let arrayMerged = Array(zip(xaxis, yaxis))

1 Comment

The latter answer is 👌 I wounder which is faster?
2

Try this:

let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]

let yaxis = [1, 2, 3, 4, 5]

var newArr = [(String, Int)]()

for i in 0..<xaxis.count {
        newArr.append((xaxis[i], yaxis[i]))
}
print(newArr)

1 Comment

This makes no use of the yaxis array.
1
let tuples = xaxis.enumerated().map { (index, value) in (value, yaxis[index]) }

Assuming yaxis's count always matches to xaxis.

2 Comments

This also assumes the values in the xaxis array are unique.
the code looks short and clean but has time complexity, if used on a large array. answer from 3stud1ant3 is recommended if considered time complexity

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.