Section 1 - Instruction

Last time, we saw how to access array elements using their index, like scores[0] for the first item. Remember, indices start at 0.

Engagement Message

What if you wanted to get the very last item in an array, especially if you don't know how long the array is?

Section 2 - Instruction

Swift has a handy solution for this! You can use the .last property to get the last element directly from any array.

The .last property always refers to the final item in the array, no matter its length.

Engagement Message

Does this seem like a useful shortcut?

Section 3 - Instruction

So, if we have:

using planets.last would give you "Earth".

This works no matter how many items are in the array. .last always gets the final one.

Engagement Message

What would colors.last give you if colors = ["red", "green", "blue"]?

Section 4 - Instruction

Swift also provides a .first property that works similarly, always giving you the first element of the array.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal