Swift Array - reverse Function



The reverse() function of an array is used to reverse the order of the elements present in the given array. This function works only for mutating collections such as arrays, it does not work with non-mutating collections. Mutating collections are those collections that can be modified whereas non-mutating collections are those collections that cannot be modified, if we try to modify them we will get an error. Also, this function doesnt change the elements it simply updates the position of the elements. The complexity of this function is O(n), where n is the number of elements in the given array.

For example, we have an array = [34, 10, 38, 9, 19]. Now using the reverse() function we reverse the order of the given array. Hence the resultant array is [19, 9, 38, 10, 34].

Syntax

Following is the syntax of the reverse() function −

func reverse()

Parameters

This function does not take any parameter.

Return Value

This function returns an array with elements in reverse order.

Now we will discuss how to use the reverse() function with the help of the following examples:

Example 1

Swift program to reverse the order of the array elements using reverse() function.

import Foundation
// Array of integer type
var numbers = [4, 19, 23, 19, 10]
print("Original Array:", numbers)

// Reverse the order of the array elements
numbers.reverse()

// Display the result
print("Reversed Array:", numbers)

// Array of string type
var stringArr = ["Hello", "Hi", "Hey"]
print("\nOriginal Array:", stringArr)

// Reverse the order of the array elements
stringArr.reverse()

// Display the result
print("Reversed Array:", stringArr)

// Array of double type
var doubleArr = [3.2, 1.0, 4.2, 5.5, 34.3]
print("\nOriginal Array:", doubleArr)

// Reverse the order of the array elements
doubleArr.reverse()

// Display the result
print("Reversed Array:", doubleArr)

Output

Original Array: [4, 19, 23, 19, 10]
Reversed Array: [10, 19, 23, 19, 4]

Original Array: ["Hello", "Hi", "Hey"]
Reversed Array: ["Hey", "Hi", "Hello"]

Original Array: [3.2, 1.0, 4.2, 5.5, 34.3]
Reversed Array: [34.3, 5.5, 4.2, 1.0, 3.2]

Example 2

Swift program to reverse only specified portion of the array using reverse() function. Here we do not reverse the whole array instead we reverse the elements present in the specified range(1..<5).

import Foundation

// Defining array of integer type
var nums = [20, 40, 50, 60, 70, 80, 10]

print("Original Array:", nums)

// Specifying a range of elements
let reverseRange = 1..<5

// Reversing the elements present in the given range
nums[reverseRange].reverse()

print("Reversed Array: \(nums)")

Output

Original Array: [20, 40, 50, 60, 70, 80, 10]
Reversed Array: [20, 70, 60, 50, 40, 80, 10]

Example 3

Swift program to reverse a matrix using reverse() function. Here we reverse the rows of the given matrix using the reverse() function.

import Foundation

var inputMatrix = [
   [1, 2, 1],
   [9, 2, 1],
   [7, 1, 4],
   [4, 7, 9],
]

// Print the original matrix
print("Original Matrix:")
for p in inputMatrix {
   print(p)
}

// Reversing the rows of the given matrix
inputMatrix.reverse()

print("\nReversed Matrix:")
for q in inputMatrix {
   print(q)
}

Output

Original Matrix:
[1, 2, 1]
[9, 2, 1]
[7, 1, 4]
[4, 7, 9]

Reversed Matrix:
[4, 7, 9]
[7, 1, 4]
[9, 2, 1]
[1, 2, 1]

Example 4

Swift program to reverse an array of custom structures. Here we reverse the array of structure objects using the reverse() function.

import Foundation

// Student structure
struct Student {
   var name: String
}

var objs = [Student(name: "Mona"), Student(name: "Joy"), Student(name: "Siya")]

// Reverse the array using reverse()
objs.reverse()

// Display reversed array
print("Reversed Array: \(objs)")

Output

Reversed Array: [main.Student(name: "Siya"), main.Student(name: "Joy"), main.Student(name: "Mona")]
Advertisements