Add an element to an array in Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Swift arrays are ordered, type-safe collections, and adding elements is one of the most common array operations. The method you choose depends on whether you are appending one value, inserting at a specific index, or merging multiple values into the array.
Use append For One Element At The End
The most direct way to add one element is append:
This is the idiomatic answer when you want to grow the array at the end.
append is efficient for normal array-building code because Swift arrays are optimized for amortized growth at the tail.
Use append(contentsOf:) For Multiple Elements
If you want to add another collection of elements, use append(contentsOf:):
You can also use +=:
Both are valid, but append(contentsOf:) is often clearer when the intent is "append this collection."
Use insert(_:at:) For A Specific Position
When the new element belongs somewhere other than the end, use insert(_:at:):
This shifts later elements to the right. Because of that shift, insertion in the middle is generally more expensive than appending at the end.
That cost matters in tight loops or large arrays, but not in ordinary application code.
Arrays Must Be Mutable
You can only add elements to an array declared with var:
To modify it:
This is a basic but important Swift rule: let creates an immutable value.
Type Safety Still Applies
Swift arrays hold values of one element type. If the array is [Int], you cannot append a String:
This type safety is helpful because many array mistakes are caught at compile time instead of surfacing later at runtime.
If you need mixed values, Swift expects you to model that more explicitly instead of quietly mixing unrelated types.
Performance And Capacity
Appending is usually fast, but repeated growth may cause reallocation when the array runs out of reserved storage. If you already know roughly how many elements you will add, reserve capacity up front:
This is not necessary for everyday code, but it can help in performance-sensitive loops.
Another practical pattern is appending conditionally:
That keeps array-building code clean when values may or may not exist at runtime.
Common Pitfalls
One common mistake is trying to append to an array declared with let.
Another issue is using insert(_:at:) with an invalid index, which causes a runtime trap.
A third problem is assuming += and append are interchangeable in every case. append adds one element; append(contentsOf:) and += add multiple elements from another collection.
Finally, developers sometimes overthink array growth when a simple append would be perfectly fine. Most application code should start with the straightforward API and optimize only if profiling says it matters.
Summary
- Use
appendto add one element to the end of a Swift array. - Use
append(contentsOf:)or+=to add multiple elements. - Use
insert(_:at:)when the new element belongs at a specific position. - Arrays must be declared with
varif you want to modify them. - Reserve capacity only when you have a real performance reason to do so.

