Open In App

How to Remove the First element from an Array in Swift?

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Swift provides various generic collections and array is one of them. Just like other programming languages in Swift also an array is an ordered collection that is used to store the same type of values like int, string, float, etc., you are not allowed to store the value of another type in an array, for example, you have an int type array, in this array you are not allowed to store float values, if you try to do so you will get an error. It also stores duplicate values of the same type. When an array is assigned to a variable then that array is mutable which means you are allowed to change the content and the size of the array. And when an array is assigned to a constant then that array is immutable which means you are not allowed to change the content and size of the array. In the Swift array, we are allowed to remove the first element from the given array. To do this task we use the removeFirst() function. This function deletes or removes the very first element present in the array. We can also remove multiple starting elements from the array using the removeFirst() function.

Syntax:

arrayName.removeFirst(x: Int)

Here, 

arrayName is the object of the array class.

Parameter: This function takes only one parameter. It  is an optional parameter and it is used to remove the number of elements from the starting of the array

Return Value: This function returns the removed value

Example:

Swift




// Swift program to remove first element from the array
import Swift
  
// Creating an array of Authors Name
// Here the array is of string type
var AuthorName = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
  
print("Author's name before removing first element:", AuthorName)
  
// Removing the first element from the AuthorName array
// Using removeFirst() function
AuthorName.removeFirst()
  
// Displaying the final result
print("Author's name removing first element:", AuthorName)
  
// Creating an array of article numbers
// Here the array is of int type
var ArticleNumber = [23, 1, 90, 3, 56, 23, 0, 6, 12]
print(".................................")
  
print("Article numbers before removing first element:", ArticleNumber)
  
// Removing the first element from the ArticleNumber array
// Using removeFirst() function
ArticleNumber.removeFirst()
  
// Displaying the final result
print("Article numbers after removing first element:", ArticleNumber)


Output:

Author's name before removing first element: ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
Author's name removing first element: ["Poonam", "Punit", "Bittu", "Mohit"]
.................................
Article numbers before removing first element: [23, 1, 90, 3, 56, 23, 0, 6, 12]
Article numbers after removing first element: [1, 90, 3, 56, 23, 0, 6, 12]

Removing multiple elements from the array

In Swift, using the removeFirst() function we can also remove multiple elements from the beginning of the array. To do this we only need to pass the number which represents the total number of elements to remove from the array in the function. For example, if we want to remove starting 2 elements from the array then we need to pass 2 in this function. 

Syntax:

arrayName.removeFirst(3)

Example:

Swift




// Swift program to remove multiple elements from 
// the beginning of the array
import Swift
  
// Creating an array of Authors Name
// Here the array is of string type
var AuthorName = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
  
print("Author's name before removing first element:", AuthorName)
  
// Removing the first two elements from the AuthorName array
// Using removeFirst() function
AuthorName.removeFirst(2)
  
// Displaying the final result
print("Author's name removing first element:", AuthorName)
  
// Creating an array of article numbers
// Here the array is of int type
var ArticleNumber = [23, 1, 90, 3, 56, 23, 0, 6, 12]
print(".................................")
  
print("Article numbers before removing first element:", ArticleNumber)
  
// Removing the first four elements from the ArticleNumber array
// Using removeFirst() function
ArticleNumber.removeFirst(4)
  
// Displaying the final result
print("Article numbers after removing first element:", ArticleNumber)


Output:

Author's name before removing first element: ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
Author's name removing first element: ["Punit", "Bittu", "Mohit"]
.................................
Article numbers before removing first element: [23, 1, 90, 3, 56, 23, 0, 6, 12]
Article numbers after removing first element: [56, 23, 0, 6, 12]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads