Swift 3
Array
Remove Object
Programming
Swift Language

Removing object from array in Swift 3

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In Swift 3, arrays do not have a built-in "remove this object" method by value the way some other languages do. The usual pattern is to find the index of the element you want and then remove it with remove(at:). The exact solution depends on whether you want to remove the first match, every match, or an element selected by a custom condition.

Remove the First Matching Value

If the element type conforms to Equatable, Swift 3 gives you index(of:).

swift
1var names = ["Ana", "Ben", "Cara", "Ben"]
2
3if let index = names.index(of: "Ben") {
4    names.remove(at: index)
5}
6
7print(names)  // ["Ana", "Cara", "Ben"]

This removes only the first matching occurrence. That is often the intended behavior, but you should be explicit about it because arrays can contain duplicates.

Why Two Steps Are Needed

Swift arrays are indexed collections. remove(at:) needs an index, not a value:

swift
names.remove(at: 1)

So the workflow is:

  1. find the element's index
  2. remove the element at that index

This design makes sense because array removal changes element positions. Swift wants you to be clear about whether you are removing by position or by value.

Example with Custom Types

For custom objects or structs, conform to Equatable so index(of:) knows how to compare elements.

swift
1struct User: Equatable {
2    let id: Int
3    let name: String
4}
5
6var users = [
7    User(id: 1, name: "Ana"),
8    User(id: 2, name: "Ben"),
9    User(id: 3, name: "Cara")
10]
11
12let target = User(id: 2, name: "Ben")
13
14if let index = users.index(of: target) {
15    users.remove(at: index)
16}
17
18print(users.count)  // 2

If equality should be based only on id, define == accordingly instead of comparing every field mechanically.

Remove All Matching Values

Swift 3 predates the later convenience method removeAll(where:), so if you want to remove every matching element, use filter.

swift
1var values = [1, 2, 3, 2, 4, 2]
2values = values.filter { $0 != 2 }
3
4print(values)  // [1, 3, 4]

This creates a new filtered array and assigns it back to the variable. That is usually the cleanest answer in Swift 3 for bulk removal by value.

Remove by a Custom Condition

Sometimes you do not have an exact object to match, but instead a predicate such as "remove the first user whose id is 2." In Swift 3, use index(where:).

swift
1struct User {
2    let id: Int
3    let name: String
4}
5
6var users = [
7    User(id: 1, name: "Ana"),
8    User(id: 2, name: "Ben"),
9    User(id: 3, name: "Cara")
10]
11
12if let index = users.index(where: { $0.id == 2 }) {
13    users.remove(at: index)
14}
15
16print(users.map { $0.name })  // ["Ana", "Cara"]

This is useful when equality by full object identity is too strict or unavailable.

Arrays Are Value Types

One detail that matters in Swift is that Array is a value type. If you assign an array to another variable, Swift uses value semantics.

swift
1var original = ["a", "b", "c"]
2var copy = original
3
4copy.remove(at: 1)
5
6print(original)  // ["a", "b", "c"]
7print(copy)      // ["a", "c"]

That means removing an object from one array variable does not implicitly mutate another independent variable that was copied from it.

Be Careful with Index Validity

If you already know an index and call remove(at:), the index must be valid. Otherwise the app crashes.

Bad:

swift
var values = [10, 20, 30]
values.remove(at: 5)

Safer:

swift
1var values = [10, 20, 30]
2let index = 1
3
4if values.indices.contains(index) {
5    values.remove(at: index)
6}

When removing by value, optional binding from index(of:) or index(where:) naturally handles this safety check.

Common Pitfalls

The most common mistake is expecting a direct remove(object) method to exist on Array in Swift 3. It does not. You need an index or a filtering approach.

Another issue is forgetting that index(of:) removes only the first match when paired with remove(at:). If duplicates matter, use filter or loop deliberately.

Developers also sometimes use remove(at:) with a guessed index and crash the app when the index is out of range.

Finally, remember the version context. Advice that uses removeAll(where:) applies to newer Swift versions, not specifically to Swift 3.

Summary

  • In Swift 3, remove by value by first finding the index and then calling remove(at:).
  • Use index(of:) for Equatable element types.
  • Use index(where:) when removal depends on a custom condition.
  • Use filter when you want to remove all matching elements.
  • Make sure version-specific advice actually applies to Swift 3, not just to modern Swift.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.