Merge or concatenate two arrays in Swift


In Swift programming, we often need to merge or concatenate two arrays. So here in this tutorial, I am going to let you know how you can perform this task.

Before we go forward, I want to let you know that, there are multiple ways to join two Swift arrays. So I will let you know about some easy ways.

Using + Operator

You can use the + operator to combine two [Int] type arrays. It will create a new array that will contain all the elements of these two arrays.

Below is an example of merging two arrays using the + operator:

let numArr1 = [1,2,3,4,5]
let numArr2 = [6,7,8,9,10]

let compinerArr = numArr1 + numArr2
print(compinerArr)

Run this code online

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Let’s see another example of merging two [String] type arrays:

let colors1 = ["Black","Yellow","Blue"]
let colors2 = ["Red","Green","Purple"]

let combinedArr = colors1 + colors2
print(combinedArr)

Output:

["Black", "Yellow", "Blue", "Red", "Green", "Purple"]

Using append() method

You can use the Swift append() method to combined two arrays easily.

See the code example below where we have used the append() method to concatenate two arrays of type [Int]:

var numArr1 = [1,2,3,4,5]
var numArr2 = [6,7,8,9,10]

numArr1.append(contentsOf: numArr2)
print(numArr1)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

See another example of merging two arrays of type [String] below:

var fruits1 = ["Apple","Blueberry","Lemon"]
var fruits2 = ["Strawberry","Banana","Watermelon"]

fruits1.append(contentsOf: fruits2)
print(fruits1)

Output:

["Apple", "Blueberry", "Lemon", "Strawberry", "Banana", "Watermelon"]

Using joined() method

Swift already has an in-built joined method that can combine or concatenate two arrays of all types.

If you want to use this joined method, then you also have to use the Array method to get the final array which will be the combined array of our choice.

Below is an example of merging two [Int] type arrays:

let numArr1 = [1,2,3,4,5]
let numArr2 = [6,7,8,9,10]

let joinedArr = [numArr1,numArr2].joined()
let finalArr = Array(joinedArr)

print(finalArr)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In the above code, you can see that we have used Array() method also. This is because the joined() method separates two arrays with a separator. So to remove the separator and make it one array we have to use the Array() method.

Now see another example of combining two String type arrays:

let colors1 = ["Black","Yellow","Blue"]
let colors2 = ["Red","Green","Purple"]

let joinedArr = [colors1,colors2].joined()
let finalArr = Array(joinedArr)

print(finalArr)

Output:

["Black", "Yellow", "Blue", "Red", "Green", "Purple"]

Using flatMap() method

Using the flatMap() method also, we can merge two arrays.

Below is an example of merging two arrays of type [Int] using the flatMap() method:

let listOfNumbers_1 = [4,5,6]
let listOfNumbers_2 = [7,8,9]

let combinedArray = [listOfNumbers_1, listOfNumbers_2].flatMap({ (item: [Int]) -> [Int] in
    return item
})
print(combinedArray)

Output:

[4, 5, 6, 7, 8, 9]

Below is another example of joining two arrays, but this time the arrays are of type [String]:

let cityList1 = ["Los Angeles", "Chicago", "Santa Clarita"]
let cityList2 = ["Lakewood", "Phoenix", "Fort Wayne"]

let allCities = [cityList1, cityList2].flatMap({ (item: [String]) -> [String] in
    return item
})
print(allCities)

Output:

["Los Angeles", "Chicago", "Santa Clarita", "Lakewood", "Phoenix", "Fort Wayne"]