In my app i am getting 3 arrays that's named are A,B and C. Now i want to arrange all that element in single array in this manner [a1,b1,c1,a2,b2,c2] so how can i do this? Please help me.
Asked
Active
Viewed 120 times
-5
-
3Show your tried code – Jitendra Modi Feb 15 '17 at 05:32
-
Use Zip3Sequence from my answer in this question: http://stackoverflow.com/questions/40517760/is-there-a-zip-function-to-create-tuples-with-more-than-2-elements – Alexander Feb 15 '17 at 05:47
1 Answers
1
let a = ["a1","a2","a3"]
let b = ["b1","b2","b3"]
let c = ["c1","c2","c3"]
var merged: [String] = []
(0..<max(a.count, b.count, c.count)).forEach {
if $0 < a.count { merged.append(a[$0]) }
if $0 < b.count { merged.append(b[$0]) }
if $0 < c.count { merged.append(c[$0]) }
}
merged // ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]

Leo Dabus
- 229,809
- 59
- 489
- 571
-
1You should always reserve capacity when doing a known number of appends. – Alexander Feb 15 '17 at 05:59