Why is swap sometimes implemented by passing an array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In languages like Java and C where primitives are passed by value, a swap(a, b) function cannot modify the caller's variables because it only receives copies. Passing an array (or using array indices) is a workaround: the function receives a reference to the array and can modify its elements in place. This is unnecessary in languages with pass-by-reference (C++ references, C pointers, Python objects) but is a common pattern in Java and JavaScript for primitive swaps.
The Problem: Pass-by-Value
Java passes primitives (int, double, boolean) by value. The function operates on copies, and changes do not propagate back to the caller.
Solution: Pass an Array
Java passes object references by value. The function receives a copy of the reference to the array, but both the caller and the function point to the same array. Modifying arr[i] changes the actual array contents.
Why This Works
The array itself is not copied — only the reference is. This is why element modifications are visible to the caller.
The Same Pattern in JavaScript
JavaScript Destructuring Swap
In modern JavaScript, destructuring provides a cleaner alternative for local variables:
This works inline without a function, but only for local variables.
Languages Where Arrays Are Not Needed
C++ — Pass by Reference
C++ references (int&) are aliases for the caller's variables. No array workaround needed.
C — Pass by Pointer
C does not have references, but pointers serve the same purpose. The caller passes addresses explicitly.
Python — Tuple Unpacking
Python does not need a swap function because tuple unpacking handles it natively.
Common Use Case: Sorting Algorithms
The array-based swap pattern appears naturally in sorting algorithms where you swap elements within an array:
Here the array pattern is not a workaround — it is the natural approach because the data lives in an array.
Single-Element Array Wrapper (Hack)
In Java, some developers use a single-element array as a mutable container:
This is ugly and rarely used. In practice, Java developers either swap elements within an existing array or restructure their code to avoid needing a swap function.
Common Pitfalls
- Assuming Java passes objects by reference: Java passes object references by value. You can modify an object's fields through the reference, but you cannot make the caller's variable point to a different object.
arr = new int[]{3, 4}inside a function does not affect the caller's array. - XOR swap trick:
a ^= b; b ^= a; a ^= b;swaps without a temp variable but fails whenaandbare the same memory location (result is 0). It is also slower than a temp variable on modern CPUs. Avoid it. - Forgetting bounds checking:
swap(arr, i, j)should verify thatiandjare within bounds.ArrayIndexOutOfBoundsException(Java) or undefined behavior (C/C++) results from out-of-bounds access. - Using swap on immutable types: In Java,
Stringand wrapper types (Integer,Double) are immutable. Even with an array, you cannot swap twoStringvariables without reassignment. You can swap array elements containing strings, but not standalone string variables. - Overcomplicating in languages with native swap: C++ has
std::swap, Python has tuple unpacking, Kotlin hasalso. Do not implement array-based swap in these languages — use the native mechanism.
Summary
- Passing an array to a swap function works because Java/JavaScript pass the array reference by value, allowing element modification
- This pattern is needed in languages where primitives are pass-by-value (Java, JavaScript, C)
- C++ uses references (
int&), C uses pointers (int*), and Python uses tuple unpacking (a, b = b, a) - The array swap pattern is most natural in sorting algorithms where data already lives in an array
- Use the language's native swap mechanism when available —
std::swap(C++), tuple unpacking (Python), destructuring (JavaScript)

