Menu
Coddy logo textTech

Modifying Arrays

Part of the Fundamentals section of Coddy's Swift journey — lesson 64 of 86.

Arrays become truly useful when you can change their contents. To modify an array, you must declare it with var instead of let.

You can change an existing element by assigning a new value to a specific index:

var colors = ["Red", "Green", "Blue"]
colors[1] = "Yellow"
print(colors)  // ["Red", "Yellow", "Blue"]

To add new elements, use the append method for a single item, or the += operator to add multiple items at once:

var numbers = [1, 2, 3]
numbers.append(4)
print(numbers)  // [1, 2, 3, 4]

numbers += [5, 6]
print(numbers)  // [1, 2, 3, 4, 5, 6]

To insert an element at a specific position, use the insert method:

var fruits = ["Apple", "Orange"]
fruits.insert("Banana", at: 1)
print(fruits)  // ["Apple", "Banana", "Orange"]

To remove elements, use remove(at:) with the index you want to delete:

var items = ["A", "B", "C", "D"]
items.remove(at: 2)
print(items)  // ["A", "B", "D"]

Remember, when you remove an element, all subsequent elements shift down to fill the gap.

challenge icon

Challenge

Easy
Write a function modifyInventory that takes an array of items, a new item to add, and an index to remove, then returns the modified array.

Perform the following operations in order:

  1. Change the first element to "Updated"
  2. Append the new item to the end of the array
  3. Remove the element at the specified index

Parameters:

  • items ([String]): The initial array of items
  • newItem (String): The item to append
  • removeIndex (Int): The index of the element to remove (after appending)

Returns: The modified array after all operations ([String])

Example: If items is ["A", "B", "C"], newItem is "D", and removeIndex is 2, the function should:

  1. Change first element: ["Updated", "B", "C"]
  2. Append "D": ["Updated", "B", "C", "D"]
  3. Remove at index 2: ["Updated", "B", "D"]

Cheat sheet

Arrays declared with var can be modified, while arrays declared with let are immutable.

Change an element by assigning a new value to a specific index:

var colors = ["Red", "Green", "Blue"]
colors[1] = "Yellow"  // ["Red", "Yellow", "Blue"]

Add a single element with append:

var numbers = [1, 2, 3]
numbers.append(4)  // [1, 2, 3, 4]

Add multiple elements with +=:

numbers += [5, 6]  // [1, 2, 3, 4, 5, 6]

Insert an element at a specific position with insert(_:at:):

var fruits = ["Apple", "Orange"]
fruits.insert("Banana", at: 1)  // ["Apple", "Banana", "Orange"]

Remove an element at a specific index with remove(at:):

var items = ["A", "B", "C", "D"]
items.remove(at: 2)  // ["A", "B", "D"]

When an element is removed, all subsequent elements shift down to fill the gap.

Try it yourself

func modifyInventory(items: [String], newItem: String, removeIndex: Int) -> [String] {
    // Write code here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals