The dropLast(:_)
method is used to remove a given number of elements at the end of the array and return a new array without those elements.
arr.dropLast(n)
n
: This is the number of elements to drop off at the end of the array. It must be greater than or equal to zero.
This method returns new array that contains elements of the original array, except for the last n
elements.
// create arrayslet numbers = [2, 4, 5, 100, 45]let fruits = ["orange", "mango", "grape"]let languages = ["Swift", "Python", "Java", "C", "C++"]let dates = [2022, 2012, 1998, 1774]// invoke the dropLast(:_) method// and print resultsprint(numbers.dropLast(2)) // [2, 4, 5]print(fruits.dropLast(1)) // ["orange", "mango"]print(languages.dropLast(4)) // ["Swift"]print(dates.dropLast(0)) // [2022, 2012, 1998, 1774]
dropLast(_:)
method on the arrays and print the results to the console.