Swift Program to Concatenate Two Arrays

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to concatenate two arrays.

An array is an ordered collection which is used to store same type of data. It can be immutable or mutable. In an array, concatenation is process of creating a new array by merging two or more arrays.

To concatenate two arrays always remember following points −

Both the array must be of same type.

Array must be mutable.

Below is a demonstration of the same −

Suppose our given input is −

Array1 = [93, 82, 73]
Array2 = [839, 84, 23]

The desired output would be −

New Array = [93, 82, 73, 839, 84, 23]

In Swift, we can concatenate arrays using any of the following method −

Method 1- Using (+) Operator

We can concatenate two or more arrays with the help of + operator. It create a new array to combine two or more array.

Syntax

Following is the syntax −

newArray = arr1 + arr2

Example

The following program shows how to concatenate two arrays using + operator.

import Foundation import Glibc var myArray1 : [Int] = [83, 9, 22, 45, 23, 3, 45] var myArray2 : [Int] = [98, 34, 2, 1, 5, 6] // Concatenating two arrays var newArray = myArray1 + myArray2 print("Array 1:", myArray1) print("Array 2:", myArray2) print("Merged Array: ", newArray)

Output

Array 1: [83, 9, 22, 45, 23, 3, 45]
Array 2: [98, 34, 2, 1, 5, 6]
Merged Array: [83, 9, 22, 45, 23, 3, 45, 98, 34, 2, 1, 5, 6]

Here, in the above code, we have two arrays myArray1 and myArray2. Now we concatenate them using + operator and store the result into new array −

var newArray = myArray1 + myArray2

So the output is [83, 9, 22, 45, 23, 3, 45, 98, 34, 2, 1, 5, 6]

Method 2- Using (+=) Operator

We can concatenate two or more arrays with the help of += operator.

Syntax

Following is the syntax −

arr1 += arr2

Example

The following program shows how to concatenate two arrays using += operator.

import Foundation import Glibc var myArray1 : [Int] = [3, 5, 7, 8, 56, 76] var myArray2 : [Int] = [2, 1, 5, 6] print("Array 1:", myArray1) print("Array 2:", myArray2) // Concatenating two arrays myArray1 += myArray2 print("Merged Array: ", myArray1)

Output

Array 1: [3, 5, 7, 8, 56, 76]
Array 2: [2, 1, 5, 6]
Merged Array: [3, 5, 7, 8, 56, 76, 2, 1, 5, 6]

Here, in the above code, we have two arrays myArray1 and myArray2. Now we concatenate them using + = operator. This operator does not create a new array, instead it add all the elements of myArray2 at the end of myArray1.

myArray1 += myArray2

So the output is [3, 5, 7, 8, 56, 76, 2, 1, 5, 6]

Method 3- Using joined() Function

Swift provide an in-built function named as joined() to join all the elements of two arrays. This function return the result in FlattenSequence type which is little hard to read, so to get an easy to read output we wrap the result of joined() into a new array using Array().

Syntax

Following is the syntax −

newArray = [arr1, arr2].joined()

Example

The following program shows how to concatenate two arrays using joined() function.

import Foundation import Glibc var myArray1 : [Int] = [3, 5, 7, 8, 56, 76] var myArray2 : [Int] = [2, 1, 5, 6] print("Array 1:", myArray1) print("Array 2:", myArray2) // Concatenating two arrays var newArray = [myArray1, myArray2].joined() var result = Array(newArray) print("Merged Array: ", result)

Output

Array 1: [3, 5, 7, 8, 56, 76]
Array 2: [2, 1, 5, 6]
Merged Array: [3, 5, 7, 8, 56, 76, 2, 1, 5, 6]

Here, in the above code, we have two arrays myArray1 and myArray2. Now we concatenate them using joined function −

var newArray = [myArray1, myArray2].joined()

And wrap the result of joined() into a new array using Array().

var result = Array(newArray)

So the output is [3, 5, 7, 8, 56, 76, 2, 1, 5, 6]

Method 4- Using append() Function

Swift provide an in-built function named as append() to concatenate two arrays. This function adds new array(arr2) at the end of the original array(arr1).

Syntax

Following is the syntax −

arr1.append(contentsOf:arr2)

Example

The following program shows how to concatenate two arrays using append() function.

import Foundation import Glibc var myArray1 : [String] = ["mona", "lisa", "siona"] var myArray2 : [String] = ["Medu", "Mohana"] print("Array 1:", myArray1) print("Array 2:", myArray2) // Concatenating two arrays myArray1.append(contentsOf: myArray2) print("Merged Array: ", myArray1)

Output

Array 1: ["mona", "lisa", "siona"]
Array 2: ["Medu", "Mohana"]
Merged Array: ["mona", "lisa", "siona", "Medu", "Mohana"]

Here, in the above code, we have two arrays myArray1 and myArray2. Now we concatenate them using append() function. This function does not create a new array, instead it add all the elements of myArray2 at the end of myArray1.

myArray1.append(contentsOf: myArray2)

So the output is ["mona", "lisa", "siona", "Medu", "Mohana"]

Method 5- Using zip() Function

Swift provide an in-built function named as zip() to concatenate two arrays. This function merge two arrays into a single sequence of a tuple. This function return the result in Zip2Sequence type which is little hard to read, so to get an easy to read output we wrap the result of joined() into a new array using Array().

Syntax

Following is the syntax −

newArray = zip(arr1, arr2)

Example

The following program shows how to concatenate two arrays using zip() function.

import Foundation import Glibc var myArray1 : [String] = ["mona", "lisa", "siona"] var myArray2 : [String] = ["Medu", "Mohana"] print("Array 1:", myArray1) print("Array 2:", myArray2) // Concatenating two arrays var newArr = zip(myArray1, myArray2) print("Merged Array: ", Array(newArr))

Output

Array 1: ["mona", "lisa", "siona"]
Array 2: ["Medu", "Mohana"]
Merged Array: [("mona", "Medu"), ("lisa", "Mohana")]

Here, in the above code, we have two arrays myArray1 and myArray2. Now we concatenate them using zip() function. And get easy to read output using Array().

raja
Updated on 11-Oct-2022 12:05:41

Advertisements