7
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
  • 61,578
  • 25
  • 134
  • 173
Muzahid
  • 5,072
  • 2
  • 24
  • 42

2 Answers2

18

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
  • 70,367
  • 12
  • 127
  • 192
-3

Give this a shot

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

   var c = a + b
   print(c)
Erik
  • 2,299
  • 4
  • 18
  • 23