How to merge two arrays in Swift?

Just like other programming languages, Swift also provides array structures for storing data elements of the same type. Once you are familiar with declaring and storing elements in such an array, you might want to perform simple and complex operations on the same.
In this tutorial,  you will learn how to merge two arrays in Swift.

Different ways to merge two arrays in Swift

Using append(contentsOf:) method:

Let us consider two sample arrays as follows:

var eg_array_1:[Int] = [1, 100]
var eg_array_2:[Int] = [2, 99]
print("Array 1: \(eg_array_1)")
print("Array 2: \(eg_array_2)")
Array 1: [1, 100]
Array 2: [2, 99]

We can append the second array to the first by using append(contentsOf:) as follows;

var eg_array_1:[Int] = [1, 100]
var eg_array_2:[Int] = [2, 99]
print("Array 1: \(eg_array_1)")
print("Array 2: \(eg_array_2)")
eg_array_1.append(contentsOf: eg_array_2)
print("The merged array: \(eg_array_1)")
Array 1: [1, 100]
Array 2: [2, 99]
The merged array: [1, 100, 2, 99]

Using + operator:

You can use the ‘+‘ symbol to create a new array by combining the other two arrays.
An example of the same is as follows;

var eg_array_1:[Int] = [1, 100]
var eg_array_2:[Int] = [2, 99]
print("Array 1: \(eg_array_1)")
print("Array 2: \(eg_array_2)")
var merged_array=eg_array_1+eg_array_2
print("The merged array: \(merged_array)")
Array 1: [1, 100]
Array 2: [2, 99]
The merged array: [1, 100, 2, 99]

Using += operator to merge two arrays in Swift

It is one of the shortest ways to merge two arrays and is as shown;

var eg_array_1:[Int] = [1, 100]
var eg_array_2:[Int] = [2, 99]
print("Array 1: \(eg_array_1)")
print("Array 2: \(eg_array_2)")
eg_array_1+=eg_array_2
print("The merged array: \(eg_array_1)")
Array 1: [1, 100]
Array 2: [2, 99]
The merged array: [1, 100, 2, 99]

Using joined() method to merge two arrays in swift:

The joined() method is an in-built used to combine the elements of two arrays. On using this method, you should also further use the Array() method to get the combined array by removing the separator added by the joined().

var eg_array_1:[Int] = [1, 100]
var eg_array_2:[Int] = [2, 99]
print("Array 1: \(eg_array_1)")
print("Array 2: \(eg_array_2)")
var merged_array=[eg_array_1,eg_array_2].joined()
print("The merged array: \(Array(merged_array))")
Array 1: [1, 100]
Array 2: [2, 99]
The merged array: [1, 100, 2, 99]

Note:

The above methods stand true for all types of arrays i.e., regardless of the data type of the elements that a particular array holds.
You can look into an example of merging two string arrays below;

var eg_array_1:[String] = ["One", "Two"]
var eg_array_2:[String] = ["Three", "Four"]
print("Array 1: \(eg_array_1)")
print("Array 2: \(eg_array_2)")
var merged_array=eg_array_1+eg_array_2
print("The merged array: \(merged_array)")
Array 1: ["One", "Two"]
Array 2: ["Three", "Four"]
The merged array: ["One", "Two", "Three", "Four"]

Also;
Do remember that if you are appending an array to another for merging the two of them, that array must be mutable. That is, it must be declared under a mutable type.

You can also check out How to shuffle array elements in Swift

Leave a Reply

Your email address will not be published.