How to find index of list item in Swift?
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
Finding an item index in Swift arrays is simple for basic cases, but robust code needs to handle optionals safely, custom matching logic, and performance under repeated lookups. Swift provides clear standard library APIs for each scenario. This guide covers practical patterns and when to use each one.
Use firstIndex(of:) for Equatable Values
If elements conform to Equatable, use firstIndex(of:) for direct value lookup.
The returned index is optional because the value may not exist.
Use firstIndex(where:) for Custom Conditions
When matching rules are not plain equality, use a predicate closure.
This keeps business matching logic explicit and easy to review.
Normalize Strings for User Input Searches
User-entered text often differs in case and spacing. Normalize both source and target before comparison.
This pattern prevents subtle mismatch bugs in search fields.
Collect All Matching Indices When Needed
Sometimes you need every matching position, not just the first.
This is useful in batch edits and analytics tasks.
Speed Up Repeated Lookups with an Index Map
Array index searches are linear. If you perform many lookups, precompute a dictionary from value to first index.
This trades memory for faster repeated reads.
Safety and Mutation Considerations
Indices are tied to array state. If elements are inserted or removed, previously stored indices may no longer point to the same item.
Good practice:
- Recompute index after structural mutations.
- Avoid force-unwrapping index optionals.
- Treat indices as short-lived values.
This prevents stale-index bugs that are difficult to reproduce.
Generic Helper Pattern
A generic helper keeps lookup behavior consistent across the codebase.
Use this only if your project prefers sentinel integers over optional indices.
Common Pitfalls
- Force-unwrapping optional indices and crashing on missing values.
- Repeatedly scanning large arrays in hot paths instead of using lookup maps.
- Ignoring case or whitespace normalization for user-entered text.
- Using stale indices after array mutations.
- Overusing custom search helpers when standard library methods are already clear.
Summary
- Use
firstIndex(of:)for directEquatablevalue searches. - Use
firstIndex(where:)for predicate-based matching. - Normalize strings for robust user-facing text searches.
- Build index maps for high-frequency lookup workloads.
- Handle optional indices safely and refresh indices after array changes.
Related reading
- How to find ith item in zigzag ordering?
- How to find list intersection?
- How to find max. and min. in array using minimum comparisons?
- How to find maximum spanning tree?
- How to find NSDocumentDirectory in Swift?
- How to find NSDocumentDirectory in Swift?
- How to find minimum number of jumps to reach the end of the array in On time
- How to find mother vertex in a directed graph in Onm?

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.