
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Last Given Number of Items from Array in Swift
In this article, we will learn how to write a swift program to remove the last given number of items from the array.
An array is used to store elements of same data type in an order. So, here we use the following methods to remove the last given number of items from the array ?
Using removeLast(_:) function
Using dropLast(_:) function
Using removeSubrange() function
Method 1: Using removeLast(_:) Function
Swift provide an in-built function named as removeLast() function to remove the specified number of items from the end of the array.
Syntax
func removeLast(_x: Int)
Here, x is the total number of elements which we wants to remove from the end of the given collection. The value of x must be greater than or equal to 0 and must not exceed the number of elements in the given array.
Algorithm
Step 1 ? Create an array with values.
Step 2 ? Declare a variable to store the number. Here, the number represent the total number of elements which we want to remove at the end of the array.
Step 3 ? Remove the last elements from the given array using removeLast() function.
myArray.removeLast(x)
Step 4 ? Print the modified array.
Example
Following Swift program to remove the last given number of items from the array.
import Foundation import Glibc // Creating an array of integer type var myArray = [23, 5, 6, 77, 12, 11, 33, 445, 32, 3] print("Original Array:", myArray) let x = 4 // Removing last elements myArray.removeLast(x) print("Modified Array:", myArray)
Output
Original Array: [23, 5, 6, 77, 12, 11, 33, 445, 32, 3] Modified Array: [23, 5, 6, 77, 12, 11]
Here in the above code, we have an array of integer type. Now using removeLast() function we remove the last four elements from the given array and display the modified array.
Method 2: Using dropLast(_:) function
We can also use dropLast() function to remove the last given number of items from the array. This function will return an empty array if the number of elements which we want to drop is exceeds the total number of elements present in the given collection.
Syntax
func dropLast(_y: Int)
Here, y is the total number of elements which we wants to remove from the end of the given collection. The value of x must be greater than or equal to 0.
Algorithm
Step 1 ? Create an array with values.
Step 2 ? Declare a variable to store the number. Here, the number represent the total number of elements which we want to remove from the end of the array.
Step 3 ? Remove the last elements from the given array using dropLast() function.
myArray.dropLast(x)
Step 4 ? Print the modified array.
Example
Following Swift program to remove the last given number of items from the array.
import Foundation import Glibc // Creating an array of integer type var myArray = [73, 23, 1, 34, 5, 6, 7, 8, 9, 10, 49] print("Original Array:", myArray) let x = 5 // Removing last elements var result = myArray.dropLast(x) print("Modified Array:", result)
Output
Original Array: [73, 23, 1, 34, 5, 6, 7, 8, 9, 10, 49] Modified Array: [73, 23, 1, 34, 5, 6]
Here in the above code, we have an array of integer type. Now we use dropLast() function to remove last 5 elements from the given array and display the output.
Method 3: Using removeSubrange(_:) function
To remove the last given number of items from the array we can use removeSubrange() function. This function will remove all the elements in the specified subrange from the given array.
Syntax
func removeSubrange(_x: Range<Self.Index>)
Here, x is the range of the collection which we want to remove. The value of x must be a valid indices of the given sequence.
Algorithm
Step 1 ? Create an array of integer type.
Step 2 ? Print the original array.
Step 3 ? Remove the last given number of elements from the array using removeSubrange() function.
Step 4 ? Print the output.
Example
Following Swift program to remove the last given number of items from the array.
import Foundation import Glibc // Creating an array of integer type var Number = [4, 56, 88, 34, 2, 1, 13, 25, 56, 11] print("Original Array:", Number) let x = 5 // Removing last elements Number.removeSubrange(Number.count-x..<Number.count) print("Modified Array:", Number)
Output
Original Array: [4, 56, 88, 34, 2, 1, 13, 25, 56, 11] Modified Array: [4, 56, 88, 34, 2]
Here in the above code, we have an array of integer type. Now we use removeSubrange() function to remove last 5 elements from the given array and display the output.
Conclusion
So this is how we can remove the last given number of elements from the array. Here, removeLast(_:) and removeSubrsange() functions modify the original array but dropLast() function does not modify the original array, it creates a new resultant array. So if you does not want to modify original array you can use dropLast() function to remove last given number of elements from the array.