Swift program to reverse an array

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to reverse an array.

An array is an ordered collection which is used to store same type of data. For example, if any array is of integer type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc. It can be mutable or immutable.

Syntax

Following is the syntax of an array −

var myArray = [Type]()

Or

var myArray = [value1, value2, value3]

In an Array, we can reverse the elements of the array with the help of the following in built functions.

Below is a demonstration of the same −

Input

Suppose our given input is −

myArr = [ 34,56, 78, 2, 44, 67]

Output

The desired output would be −

Reversed array = [ 67, 44, 2, 78, 56, 34]

Method 1 - Using reverse() function

In Swift, we can reverse an array using in built function named reverse(). This function reverse the element s of the given array. It reverse the element in the same array means it does not create a separate array to store reversed elements.

Syntax

Following is the syntax of an array −

arrName.reverse()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array.

  • Step 2− Reverse the array using reverse() function

myArr.reverse()
  • Step 3− Print the output.

Example

The following program shows how to reverse an array.

import Foundation import Glibc // Array of string type var myArr = ["cream", "butter", "popcorn", "bread"] print("Original array:", myArr) // Reverse the array // Using reverse() function myArr.reverse() print("Reversed array:", myArr)

Output

Original array: ["cream", "butter", "popcorn", "bread"] 
Reversed array: ["bread", "popcorn", "butter", "cream"]

Here, in the above code, we have an array of String type and we reverse the array using reverse() function and display the output - ["bread", "popcorn", "butter", "cream"].

Method 2 - Using reversed() function

In Swift, we can reverse an array using in built function named reversed(). This function reverse the elements of the given array. It creates a new array to store the reversed elements of the array and return the new array.

Syntax

Following is the syntax of an array −

arrName.reversed()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array.

  • Step 2− Reverse the array using reversed() function and the result stored in a new array.

var result : [Int] = myArr.reversed()
  • Step 3− Print the output.

Example

The following program shows how to reverse an array.

import Foundation import Glibc // Array of Int type var myArr = [34, 87, 23, 1, 98, 3] print("Original array:", myArr) // Reverse the array // Using reversed() function var result : [Int] = myArr.reversed() print("Reversed array:", result)

Output

Original array: [34, 87, 23, 1, 98, 3] 
Reversed array: [3, 98, 1, 23, 87, 34]

Here, in the above code, we have an array of Int type and we reverse the array using reversed() function and store the result into new array. So the output − [ 3,98, 1, 23, 87,34]

raja
Updated on 10-Oct-2022 12:58:07

Advertisements