Swift Array – Delete First N Elements

To delete first N elements of an Array in Swift, call dropFirst() method and pass N (integer) as argument to dropFirst() method. dropFirst() method returns a new array with N elements dropped from the starting of given array.

The syntax to call dropFirst() method on the array is

var result = arr.dropFirst(N)

where N is an integer, and represents the number of elements we would like to drop from the array at starting end.

Example

In the following program, we will initialize an array with some values, and delete the first two elements using dropFirst() method.

main.swift

var arr = [1, 99, 6, 74, 5]
let N = 2
let result = arr.dropFirst(N)
print("Original  Array: \(arr)")
print("Resulting Array: \(result)")

Output

Swift Array - Delete First N Elements
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to get the subsequence which contains the elements of given array, with the first N elements dropped.