6
let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]

How do merge two array so that the out put would be ["Albert", "Charles", "Bobby", "David"]

nhgrif
  • 60,018
  • 25
  • 128
  • 167
Muzahid
  • 4,702
  • 2
  • 22
  • 36

2 Answers2

17

You can use zip to combine your two arrays, and thereafter apply a .flatMap to the tuple elements of the zip sequence:

let array1 = ["Albert","Bobby"]
let array2 = ["Charles", "David"]

let arrayMerged = zip(array1,array2).flatMap{ [$0.0, $0.1] }

print(arrayMerged) // ["Albert", "Charles", "Bobby", "David"]
dfrib
  • 65,637
  • 11
  • 118
  • 178
-3

Give this a shot

   var a = ["one", "two"]
   var b = ["three", "four"]

   var c = a + b
   print(c)
Erik
  • 2,259
  • 4
  • 17
  • 23