Swift Array - removeFirst Function



The removeFirst() function is a predefined array function. It is used to remove the first element from the given array. It works only with mutating arrays because mutating arrays can be modified whereas we can not modify a non-mutating array, if we try to do we will get an error.

For example, we have an array = [30, 40, 50, 60]. Now using the removeFirst() function we will remove the first element of the given array. Hence the resultant array is [40, 50, 60].

We can use the removeFirst() function in two different ways −

  • removeFirst() function
  • removeFirst(_:) function

Swift Array removeFirst() Function

The removeFirst() function is used to remove the first element from the specified array. It removes only one element at a time. The complexity of this function is O(n), where n represents the length of the given array.

Syntax

Following is the syntax of the removeFirst() function −

func removeFirst() -> Self.Element

Parameters

This function does not take any parameter.

Return Value

This function returns and removes the first element of the given array.

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

Example 1

Swift program to remove the first elements from the given arrays using the removeFirst() function.

import Foundation
// Defining an array of integer type
var array1 = [2, 19, 10, 11, 12, 143]
print("Original Array:", array1)

// Removing first element from the array using removeFirst() function
array1.removeFirst()
print("Array after removal:", array1)

// Defining an array of string type
var array2 = ["abc", "def", "ghf", "ljk", "mno"]
print("\nOriginal Array:", array2)

// Removing the first element from the array using the removeFirst() function
array2.removeFirst()
print("Array after removal:", array2)

Output

Original Array: [2, 19, 10, 11, 12, 143]
Array after removal: [19, 10, 11, 12, 143]

Original Array: ["abc", "def", "ghf", "ljk", "mno"]
Array after removal: ["def", "ghf", "ljk", "mno"]

Example 2

Swift program to remove the first element from the given array only if the given condition is true.

import Foundation
var nums = [11, 22, 33, 44, 55, 66, 77]

print("Original Array:", nums)

// Remove elements from the beginning of the array if they are less than 50
while !nums.isEmpty && nums.first! < 50 {
   nums.removeFirst()
}
print("Array after removal:", nums)

Output

Original Array: [11, 22, 33, 44, 55, 66, 77]
Array after removal: [55, 66, 77]

Swift Array removeFirst(_: ) Function

The removeFirst(_:) function is used to remove a specific number of elements from the start of the given array. This function removes multiple elements from the array at a time. The complexity of this function is O(n), where n represents the length of the array.

Syntax

Following is the syntax of the removeFirst(_:) function −

func removeFirst(_X: Int)

Parameters

This function takes only one parameter which is X. Here X represents the total number of elements we want to remove from the beginning of the array.

Return Value

This function returns removed elements.

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

Example 1

Swift program to remove the initial elements from the given arrays using the removeFirst() function.

import Foundation
// Defining an array of double type
var array1 = [2.34, 6.64, 9.876, 3.21, 6.5]
print("Original Array:", array1)

// Removing the first 2 elements from the array using the removeFirst() function
array1.removeFirst(2)
print("Array after removal:", array1)

// Defining an array of mixed type
var array2: [Any] = ["hello", 12, 22.3, true, 98]
print("\nOriginal Array:", array2)

// Removing the first 3 elements from the array using the removeFirst() function
array2.removeFirst(3)
print("Array after removal:", array2)

Output

Original Array: [2.34, 6.64, 9.876, 3.21, 6.5]
Array after removal: [9.876, 3.21, 6.5]

Original Array: ["hello", 12, 22.3, true, 98]
Array after removal: [true, 98]

Example 2

Swift program to remove two elements from the beginning of each row from the given matrix.

import Foundation
// Function to remove two elements from the beginning of each row
func removeElements(matrix: inout [[Int]], elementToRemove: Int) {
   for x in matrix.indices {

      // Checking if the row has enough items to remove
      if matrix[x].count >= elementToRemove {
         matrix[x].removeFirst(elementToRemove)
      } else {
         print("Matrix doesnot have enough elements to remove")
      }
   }
}

// Input matrix
var mat = [
   [11, 22, 33, 44, 55],
   [66, 77, 88, 99, 11],
   [44, 22, 55, 11, 66],
]

print("Original Matrix:")
for p in mat {
   print(p)
}

// Calling the above function to remove elements
removeElements(matrix: &mat, elementToRemove: 2)

print("Updated Matrix:")
for q in mat {
   print(q)
}

Output

Original Matrix:
[11, 22, 33, 44, 55]
[66, 77, 88, 99, 11]
[44, 22, 55, 11, 66]
Updated Matrix:
[33, 44, 55]
[88, 99, 11]
[55, 11, 66]
Advertisements