Get the first N elements of an array in Swift


In Swift, you can get the first N elements from an array in a number of ways. For example, you may need to get the first 3 elements from a Swift array.

Let’s see how we can perform our task in multiple ways:

With the help of the Subscript with a range

We can easily get the first specific number of elements from our Swift array using the subscript with range.

Using closed range

In the example below, you can see that we are retrieving the first three elements from our array using a closed range operator:

let nameArray = ["Rana", "Taylor", "John", "Oliver", "William"]
let slicedArray = nameArray[0...2]

print( slicedArray )

Output:

[“Rana”, “Taylor”, “John”]

In the output, you can see that the first three elements are only taken in our new array.

Closed range with one-sided ranges

Now see another example below using one-sided ranges closed range operator:

let nameArray = ["Rana", "Taylor", "John", "Oliver", "William"]
let slicedArray = nameArray[...2]

print( slicedArray )

In this example, there is only one difference from the previous one. Here, we have used [...2] instead of [0...2].

Half-open range

Let’s see an example of performing this task with the operator half-open:

let nameArray = ["Bonod", "Faruque", "Samim", "Oliver", "William"]
let slicedArray = nameArray[0..<3]

Half-Open Range Operator with One-Sided Ranges

let nameArray = ["Bonod", "Faruque", "Samim", "Oliver", "William"]
let slicedArray = nameArray[..<3]

In all of our above examples, it has generated the same arraySlice of the first 3 array elements of the nameArray array.

Well, which one do you prefer?

If you ask me, I would say that I prefer the half-open range operator personally. This is because the operator is (0..<3) which is easy to understand and relevant because we want to get the first three items and it contains the number 3.

Well, it is up to your personal choice which one of the methods you want to apply. All the above examples work in the same manner.

Get first N elements using a prefix

In Swift, a prefix will return a subsequence up to the specified maximum length from the array. For example, suppose we want to get the first N elements from our Swift array, then we will use the prefix(N).

Below is how we are getting the first 3 elements using the prefix():

let nameArray = ["Bonod", "Faruque", "Samim", "Oliver", "William"]
let slicedArray = nameArray.prefix(3)

// ["Bonod", "Faruque", "Samim"]

The good thing about using prefix() is that, if the parameter exceeds the maximum length of the array, it will still work and the result will contain all the elements.

Below is the example where the parameter is greater than the maximum length:

let nameArray = ["Bonod", "Faruque", "Samim", "Oliver"]
let slicedArray = nameArray.prefix(5)

// ["Bonod", "Faruque", "Samim", "Oliver"]

As you can see, we have 4 elements in our array, but we pass 5 in the prefix parameter. But the result shows 4 elements because there are only 4 elements in the array.

Using the prefix(upTo:) and prefix(through:)

Using the prefix(upTo:) is very close to using the subscript with a partial half-open range. Below is an example:

let nameArray = ["Bonod", "Faruque", "Samim", "Oliver", "William"]
let arraySlice = nameArray.prefix(upTo: 3)
// ["Bonod", "Faruque", "Samim"]

It is very close to using nameArray[..<3].

Below is another example of using the prefix(through:) which is equivalent to nameArray[...2]:

let nameArray = ["Bonod", "Faruque", "Samim", "Oliver", "William"]
let arraySlice = nameArray.prefix(through: 2)
// ["Bonod", "Faruque", "Samim"]

That’s it…

We have successfully been able to get the first N items from our Swift array.