
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
swift Array dropFirst() Function
The dropFirst() function of an array is used to remove the specified number of elements from the beginning of the given array. Using this function we can remove as many as element we want from the beginning of the array. If the number of elements we want to remove is greater than the size of the array, then this function will return an empty array. The complexity of this function is O(1).
For example, we have an array = [10, 13, 18, 19]. Now using the dropFirst(2) function we removed the first 2 elements from the array. Hence the resultant array is [18, 19].
Syntax
Following is the syntax of the dropFirst() function −
func dropFirst(_x: Int) -> Self.SubSequence
Parameters
This function takes only one parameter which is x. Here x represents the number of elements we want to remove from the beginning of the given array. If this function does not contain any parameter, then it will remove the first element from the given array.
Return value
This function returns an array after removing the initial elements.
Now we will discuss how to use the dropFirst() function with the help of the following examples:
Example 1
Swift program to demonstrate the dropFirst() function. Here we will see how the dropFirst() function works with different data types.
import Foundation // Array of string type var arrayOne = ["mango", "apple", "orange", "graps"] print("Original array:", arrayOne) // Removing first two elements using dropFirst() function print("Modified array:\(arrayOne.dropFirst(2))") // Array of integer type var arrayTwo = [8, 3, 1, 9, 3] print("\nOriginal array:", arrayTwo) // Removing first three elements using dropFirst() function print("Modified array:\(arrayTwo.dropFirst(3))") // Array of double type var arrayThree = [1.2, 25.3, 60.3, 7.2, 83.4, 10.33] print("\nOriginal array:", arrayThree) // Removing first element using dropFirst() function print("Modified array:\(arrayThree.dropFirst(1))")
Output
Original array: ["mango", "apple", "orange", "graps"] Modified array:["orange", "graps"] Original array: [8, 3, 1, 9, 3] Modified array:[9, 3] Original array: [1.2, 25.3, 60.3, 7.2, 83.4, 10.33] Modified array:[25.3, 60.3, 7.2, 83.4, 10.33]
Example 2
Swift program to demonstrate the different behaviour of the dropFirst() function. Here we will see how the dropFirst() function reacts when we do not pass any parameter to it and when we pass the number of elements greater than the size of the array.
import Foundation // Array of integer type var array1 = [2, 4, 5, 7] print("Original Array:", array1) // Calling dropFrist() function without parameter print("Modified array:", array1.dropFirst()) // Array of integer type var array2 = [30, 20, 70, 60, 10, 80] print("\nOriginal Array:", array2) // Calling dropFrist() function with a parameter greater than the size of the array print("Modified array:", array2.dropFirst(10))
Output
Original Array: [2, 4, 5, 7] Modified array: [4, 5, 7] Original Array: [30, 20, 70, 60, 10, 80] Modified array: []
Example 3
Swift program to remove the first two rows from the given matrix using the dropFirst() function.
import Foundation // Defining a matrix var matrix = [ [2, 5, 6, 7], [8, 3, 2, 1], [9, 2, 3, 1], [1, 1, 2, 2] ] // Printing original matrix print("Original Matrix:") for x in matrix{ print(x) } // Removing the first two rows from the matrix matrix = Array(matrix.dropFirst(2)) // Printing the modified matrix print("\nModified Matrix:") for y in matrix { print(y) }
Output
Original Matrix: [2, 5, 6, 7] [8, 3, 2, 1] [9, 2, 3, 1] [1, 1, 2, 2] Modified Matrix: [9, 2, 3, 1] [1, 1, 2, 2]