Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
array
collection
communitycreator

What is the remove(at:) method in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The remove(at:) method is used to remove an element at the specified position.

Syntax

arr.remove(at: index)

Parameters

arr: The array we want to remove an element from.

index: The position of the element we want to remove.

Return value

This method returns the removed element.

Example

// create arrays
var arr1 = [10, 1, 2, 3]
var arr2 = ["a", "b", "x", "c", "d"]
var arr3 = ["dog", "cat", "car", "cow"]

// remove some elements
// and print returned values
print(arr1.remove(at: 0))
print(arr2.remove(at: 2))
print(arr3.remove(at: 2))

// print modified arrays
print(arr1)
print(arr2)
print(arr3)
Removing elements at a specified position from an array

Explanation

  • Lines 2 and 3: We create three arrays.
  • Lines 8 to 10: We remove some elements from the array and print the returned results.
  • Lines 13 to 15: We print the modified arrays to the console.

RELATED TAGS

swift
array
collection
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring