
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
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")]