How to return first 5 objects of Array in Swift?
By far the neatest way to get the first N elements of a Swift array is using prefix(_ maxLength: Int)
:
let someArray = [1, 2, 3, 4, 5, 6, 7]
let first5 = someArray.prefix(5) // 1, 2, 3, 4, 5
This has the benefit of being bounds safe. If the count you pass to prefix
is larger than the array count then it just returns the whole array.
NOTE: as pointed out in the comments, Array.prefix
actually returns an ArraySlice
, not an Array
. In most cases this shouldn't make a difference but if you need to assign the result to an Array
type or pass it to a method that's expecting an Array
param you will need to force the result into an Array
type: let first5 = Array(someArray.prefix(5))
Update:
There is now the possibility to use prefix
to get the first n elements of an array. Check @mluisbrown's answer for an explanation how to use prefix.
Original Answer:
You can do it really easy without filter
, map
or reduce
by just returning a range of your array:
var wholeArray = [1, 2, 3, 4, 5, 6]
var n = 5
var firstFive = wholeArray[0..<n] // 1,2,3,4,5
With Swift 5, according to your needs, you may choose one of the 6 following Playground codes in order to solve your problem.
#1. Using subscript(_:)
subscript
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array[..<5]
//let arraySlice = array[0..<5] // also works
//let arraySlice = array[0...4] // also works
//let arraySlice = array[...4] // also works
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
#2. Using prefix(_:)
method
Complexity: O(1) if the collection conforms to RandomAccessCollection
; otherwise, O(k), where k is the number of elements to select from the beginning of the collection.
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array.prefix(5)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
Apple states for prefix(_:)
:
If the maximum length exceeds the number of elements in the collection, the result contains all the elements in the collection.
#3. Using prefix(upTo:)
method
Complexity: O(1)
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array.prefix(upTo: 5)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
Apple states for prefix(upTo:)
:
Using the
prefix(upTo:)
method is equivalent to using a partial half-open range as the collection's subscript. The subscript notation is preferred overprefix(upTo:)
.
#4. Using prefix(through:)
method
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array.prefix(through: 4)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
#5. Using removeSubrange(_:)
method
Complexity: O(n), where n is the length of the collection.
var array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
array.removeSubrange(5...)
print(array) // prints: ["A", "B", "C", "D", "E"]
#6. Using dropLast(_:)
method
Complexity: O(1) if the collection conforms to RandomAccessCollection
; otherwise, O(k), where k is the number of elements to drop.
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let distance = array.distance(from: 5, to: array.endIndex)
let arraySlice = array.dropLast(distance)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
let a: [Int] = [0, 0, 1, 1, 2, 2, 3, 3, 4]
let b: [Int] = Array(a.prefix(5))
// result is [0, 0, 1, 1, 2]