How can I remove the first n
elements from an array of Int
in Swift?
For example:
var array = [0, 1, 2, 3, 4, 5, 6] let n = 4
The result array contains these elements:
[4, 5, 6]
That provides two methods: one called removingDuplicates() that returns an array with duplicates removed, and one called removeDuplicates() that changes the array in place.
To filter an array in Swift: Call the Array. filter() method on an array. Pass a filtering function as an argument to the method.
Swift version: 5.6. The first() method exists on all sequences, and returns the first item in a sequence that passes a test you specify.
To get the first element of an array in Swift, access first property of this Array. Array. first returns the first element of this array. If the array is empty, then this property returns nil .
let result = Array(array.dropFirst(n))
(Thanks to KPM and WolfLink for pointing out that let result = array.dropFirst(n)
sets result
to an ArraySlice
which will not remain valid if the original array is released.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With