Swift Array - remove Function



The remove() function of an array is used to remove an element from the given array at the specified position or index. It removes one element at a time and works only with the mutating array. The index must be valid, if not we will get an error. The complexity of this function is O(n), where n is the length of the given array.

For example, we have an array = [Hello, Hi, Hey]. Now using the remove(at: 1) function we remove the element present at index 1 that is Hi. So the resultant array is ["Hello", "Hey"].

Syntax

Following is the syntax of the remove() function −

func remove(at index:Int)

Parameters

This function takes only one parameter which is an index. Here index represents the position from where we want to remove.

Return Value

This function returns a removed element.

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

Example 1

Swift program to remove elements from the specified arrays at the given index.

import Foundation

var numArray : [Int] = [2, 3, 12, 19, 1]
print("Original array:", numArray)

// Removing element at index 2
// Using remove(at:) function
numArray.remove(at: 2)
print("Array after removing element at index 2:", numArray)

var strArray : [String] = ["mango", "joy", "Pink"]
print("\nOriginal array:", strArray)

// Removing element at index 1
// Using remove(at:) function
strArray.remove(at: 1)
print("Array after removing element at index 1:", strArray)

var mixArray : [Any] = [100, "joy", 3.4]
print("\nOriginal array:", mixArray)

// Removing element at index 2
// Using remove(at:) function
mixArray.remove(at: 2)
print("Array after removing element at index 2:", mixArray)

Output

Original array: [2, 3, 12, 19, 1]
Array after removing element at index 2: [2, 3, 19, 1]

Original array: ["mango", "joy", "Pink"]
Array after removing element at index 1: ["mango", "Pink"]

Original array: [100, "joy", 3.4]
Array after removing element at index 2: [100, "joy"]

Example 2

Swift program to remove element at position 2 from each row in the given 2-D array.

import Foundation

// Function to remove element at position 2 from each row
func removeElement(arr: inout [[Int]]) {
   for x in 0..<arr.count {
      if arr[x].count > 2 {
         arr[x].remove(at: 2)
      } else {
         print("Row \(x + 1) does not have any element at position two")
      }
   }
}

var inputArr = [
   [1, 2, 4, 7],
   [9, 34, 1, 6],
   [10, 3, 5, 6],
]

print("Original Array:")
for y in inputArr {
   print(y)
}

// Calling the above function
removeElement(arr: &inputArr)

print("\n2D Array after removing element at position 2 in each row:")
for y in inputArr {
   print(y)
}

Output

Original Array:
[1, 2, 4, 7]
[9, 34, 1, 6]
[10, 3, 5, 6]

2D Array after removing element at position 2 in each row:
[1, 2, 7]
[9, 34, 6]
[10, 3, 6]

Example 3

Swift program to remove a row at position 1 from the given matrix using the remove(at:) function.

import Foundation

// Function to remove a row from the matrix at position 1
func removeRow(input: inout [[Int]]) {
   guard input.indices.contains(1) else {
      print("Matrix does not contain any row at index 1")
      return
   }
   input.remove(at: 1)
}

var matrix = [
   [1, 1, 4, 7],
   [9, 12, 1, 6],
   [11, 3, 5, 6],
]

print("Original Matrix:")
for y in matrix {
   print(y)
}

// Calling the above function
removeRow(input: &matrix)

print("\n2D Array after removing row  at position 1:")
for y in matrix {
   print(y)
}

Output

Original Matrix:
[1, 1, 4, 7]
[9, 12, 1, 6]
[11, 3, 5, 6]

2D Array after removing row  at position 1:
[1, 1, 4, 7]
[11, 3, 5, 6]

Example 4

Swift program to remove all the odd numbers from the given array using remove(at:) function.

import Foundation

// Function to remove odd numbers
func removeOdds(arr: inout [Int]) {

   var indexValue = 0

   while indexValue < arr.count {

      // Checking for odd numbers
      if arr[indexValue] % 2 != 0 {

         // Removing odd numbers
         arr.remove(at: indexValue)
      } else {
         indexValue += 1
      }
   }
}
var input = [1, 3, 18, 19, 23, 10, 7]

print("Original Array:", input)
removeOdds(arr: &input)
print("Array after removing odd numbers:", input)

Output

Original Array: [1, 3, 18, 19, 23, 10, 7]
Array after removing odd numbers: [18, 10]
Advertisements