How to exchange elements in swift array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Swapping two elements in a Swift array is a small operation, but it appears constantly in sorting, shuffling, and many in-place algorithms. Swift already provides the right tool for this: swapAt. Using the built-in method is clearer and safer than manually moving values around in most cases.
Use swapAt for Direct Element Exchange
The standard way to exchange two elements is:
swapAt mutates the array in place. That means the array must be declared with var, not let.
It also uses indices, so the positions must be valid for the array.
Why swapAt Is Better Than Manual Swapping
You can swap manually with a temporary variable:
That works, but swapAt expresses intent more directly:
The second version is shorter, easier to read, and less error-prone.
Check Indices Before Swapping
Like any indexed array access, swapAt will trap at runtime if either index is out of bounds. If the indices come from user input or algorithmic calculations, validate them first.
This pattern is useful when invalid indices are possible and you want to avoid a crash.
Swapping With Generic Code
Because arrays in Swift are generic collections, you can build reusable helpers that work with any element type.
This does not add functionality beyond swapAt, but it can improve readability if your domain language prefers names such as exchange or moveEndpoints.
Understand Value Semantics
Swift arrays are value types. When you pass an array normally, you pass a value, not a shared mutable object. If you want a helper function to mutate the original array, use inout.
Without inout, the function would modify only its local copy.
Swapping Is Common in Algorithms
Many in-place algorithms depend on swapping. A simple example is selection sort:
This is a good example of why clear swap operations matter. The algorithm is easier to understand when the exchange step is explicit.
What Happens If the Indices Are Equal
If you call swapAt with the same index twice, Swift simply leaves the array unchanged.
That behavior is useful because you usually do not need a separate special case when an algorithm happens to choose the same position for both sides of the swap.
Common Pitfalls
The biggest mistake is using invalid indices and causing a runtime trap. Another is declaring the array with let, which makes it immutable and impossible to swap in place. Developers also sometimes write manual temp-variable code when swapAt would be clearer. In helper functions, forgetting inout means the original array does not change. Finally, if you are swapping based on values rather than indices, you still need to find valid positions first before calling swapAt.
Summary
- Use
swapAtas the standard way to exchange two elements in a Swift array. - The array must be mutable, so declare it with
var. - Validate indices when they are not guaranteed to be safe.
- Use
inoutin helper functions that need to mutate the caller’s array. - '
swapAtkeeps algorithms such as sorting code clearer than manual temp-variable swaps.' - Swapping the same index with itself is valid and leaves the array unchanged.

