Open In App

How to Reverse an Array in Swift?

Improve
Improve
Like Article
Like
Save
Share
Report

Swift provides various generic collections and array is one of them. Just like other programming languages, in Swift also an array is an ordered collection that is used to store the same type of values like int, string, float, etc., you are not allowed to store the value of another type in an array, for example, you have an int type array in this array you are not allowed to store float values, if you try to do so you will get an error. It also stores duplicate values of the same type. When an array is assigned to a variable then that array is mutable which means you are allowed to change the content and the size of the array. And when an array is assigned to a constant then that array is immutable which means you are not allowed to change the content and size of the array. In the Swift array, we can reverse the array. To reverse the array we use the reverse() function. This function reverses the order of the specified array. It is the easiest way to reverse the array. 

Syntax:

arrayName.reverse()

Here, 

arrayName is the object of the array class.

Parameter: This function does not take any parameter.

Return Value: It does not return any value, it only updates the specified array.

Example 1:

Swift




// Swift program to reverse the array
import Swift
  
// Creating an array of number
// Here the array is of float type
var newNumber = [23.034, 1.90, 9.1, 32.34, 560.44, 21.23]
  
print("Array before reversing:", newNumber)
  
// Reverse the newNumber array
// Using reverse() function
newNumber.reverse()
  
// Displaying the final result
print("Array after reversing:", newNumber)


Output:

Array before reversing: [23.034, 1.9, 9.1, 32.34, 560.44, 21.23]
Array after reversing: [21.23, 560.44, 32.34, 9.1, 1.9, 23.034]

Example 2:

Swift




// Swift program to reverse the array
import Swift
  
// Creating an array of Emp Name
// Here the array is of string type
var GfgEmpName = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
  
print("Geeks's employee name before reversing:", GfgEmpName)
  
// Reverse the GfgEmpName array
// Using reverse() function
GfgEmpName.reverse()
  
// Displaying the final result
print("Geeks's employee name after reversing: ", GfgEmpName)
  
// Creating an array of salary
// Here the array is of int type
var GfgEmpSalary = [23000, 1000, 90000, 30000, 56000, 21000, 6000]
print(".................................")
  
print("EmpSalary before reversing:", GfgEmpSalary)
  
// Reverse the GfgEmpSalary array
// Using reverse() function
GfgEmpSalary.reverse()
  
// Displaying the final result
print("EmpSalary after reversing:", GfgEmpSalary)


Output:

Geeks's employee name before reversing: ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
Geeks's employee name after reversing:  ["Mohit", "Bittu", "Punit", "Poonam", "Sumit"]
.................................
EmpSalary before reversing: [23000, 1000, 90000, 30000, 56000, 21000, 6000]
EmpSalary after reversing: [6000, 21000, 56000, 30000, 90000, 1000, 23000]


Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads