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.
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:).
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:
So the workflow is:
- find the element's index
- 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.
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.
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:).
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.
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:
Safer:
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:)forEquatableelement types. - Use
index(where:)when removal depends on a custom condition. - Use
filterwhen you want to remove all matching elements. - Make sure version-specific advice actually applies to Swift 3, not just to modern Swift.
Related reading
- Removing viewcontrollers from navigation stack
- Rename a dictionary key
- Reorder vector using a vector of indices
- Reordering a list to maximize difference of adjacent elements
- Removing the title text of an iOS UIBarButtonItem
- Renew Provisioning Profile
- Reordering of array elements
- Repeatedly removing the maximum average subarray

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 courseTrack 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.