Swift program to remove last element from an array

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to remove last element from an Array.

An array is an ordered collection which is used to s tore same type of data. It can be mutable or immutable.

Suppose we have an array. Now we want to remove last element from the given array.

Below is a demonstration of the same −

Input

Suppose our given input is −

myArray = [3, 859, 72, 736, 42]

Remove last element

Output

The desired output would be −

New Array = [3, 859, 72, 736]

So to remove last element using any of the following methods.

Method 1 - Using removeLast() Function

To remove last element from the specified array Swift provide an in build function named rem oveLast(). It removes the last element or we can say element at index 0 from the specified array. This function only modifies the given array, it does not create a new array. It remove one element at a time.

Syntax

Following is the syntax −

ArrayName.removeLast()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array of String type with values.

  • Step 2− Remove the Last element from the array using removeLast() function.

myColor.removeLast()
  • Step 3− Print the output.

Example

The following program shows how to remove last element from the array.

import Foundation import Glibc // Creating an array of String Type var myColor = ["Blue", "Pink", "White", "Orange"] print("Original array: ", myColor) // Removing last element from the array // Using removeLast() function myColor.removeLast() print("Modified array after removing last element", myColor)

Output

Original array: ["Blue", "Pink", "White", "Orange"]
Modified array after removing last element ["Blue", "Pink", "White"]

Here in the above code, we has an array = ["Blue", "Pink", "White", "Orange"] Now from this array we remove the last element that is "Orange" using removeLast() function. So the modified array is ["Blue", "Pink", "White"].

Method 2 - Using popLast() Function

Swift provide a in b uilt function named popLast() function to remove last element from the specified array. It removes the last element from the specified array and if the array is empty, then it will return nil. It remove one element at a time.

Syntax

Following is the syntax −

ArrayName.popLast()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array of type with values

  • Step 2− Remove the Last element from the array using popLast() function.

myNumber.popLast()
  • Step 3− Print the output

Example

The following program shows how to remove last element from the array.

import Foundation import Glibc // Creating an array of Double Type var myNumber = [94.3, 944.4, 2.4, 5.6] print("Original array:", myNumber) // Removing last element from the array // Using popLast() function var res = myNumber.popLast()! print("Removed Element:", res) print("Modified array", myNumber)

Output

Original array: [94.3, 944.4, 2.4, 5.6] 
Removed Element: 5.6 
Modified array [94.3, 944.4, 2.4]

Here in the above code, we has an array = [94.3, 944.4, 2.4, 5.6]. Now from this array we remove the last element that is 5.6 using popLast() function. So the modified array is [94.3, 944.4, 2.4] .

Method 3 - Using dropLast() Function

In Swift, we can also remove last element from the specified array with the help of an in build function named dropLast( It removes the last element from the array. This function creates a new array to store the result and the original array remains the same. This function remove one element at a time.

Syntax

Following is the syntax −

ArrayName.dropLast()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array of Int type with values

  • Step 2− Remove the last element of the array using dropLast() function and store the result into a new array.

var output = myNumber.dropLast()
  • Step 3− Print the output

Example

The following program shows how to remove last element from the array.

import Foundation import Glibc // Creating an array of Int Type var myNumber = [3, 45, 87, 23, 2, 345] print("Original array: ", myNumber) // Removing last element from the array // Using dropLast() function var output = myNumber.dropLast() print("Modified array after removing the last element", output)

Output

Original array: [3, 45, 87, 23, 2, 345] 
Modified array after removing the last element [3, 45, 87, 23, 2]

Here in the above code, we has an array = [3, 45, 87, 23, 2, 345] . Now from this array we remove the last element that is 345 using myData.dropLast() function and store the result into new variable. So the output is [ 3,45, 87, 23, 2].

raja
Updated on 10-Oct-2022 13:10:09

Advertisements