Swift program to remove first element from an array

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to remove first element from an Array.

An array is an ordered collection which is used to store same type of data It can be mutable or immutable.

Suppose we have an array. Now we want to remove very first element from the given array.

Below is a demonstration of the same −

Input

Suppose our given input is −

myArray = [ 3, 859, 72, 736, 42]
Remove 1st element

Output

The desired output would be −

New Array = [ 859, 72, 736, 42]

We can remove first element using any of the following methods.

Method 1 - Using removeFirst() Function

To remove first element from the specified array Swift provide an in build function named re moveFirst(). It removes the first element or we can say element at index 0 from the array. This function only modifies the given array, it does not create a new array. This function remove one element at a time.

Syntax

Following is the syntax −

ArrayName.removeFirst()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array of Int type with values

  • Step 2− Remove the first element of the array using removeFirst() function.

myData.removeFirst()
  • Step 3− Print the output

Example

The following program s hows how to remove first element from the array.

import Foundation import Glibc // Creating array of Int Type var myData = [938, 747, 262, 94, 3, 83] print("Original array: ", myData) // Removing first element from the array // Using removeFirst() function myData.removeFirst() print("Updated array after removing first element", myData)

Output

Original array: [938, 747, 262, 94, 3, 83]
Updated array after removing first element [747, 262, 94, 3, 83]

Here in the above code, we has an array = [ 938, 747 262, 94, 3, 83]. Now from this array we remove the first element that is 938 using removeFirst() function. So the modified array is [ 747,262, 94, 3, 83].

Method 2 - Using remove() Function

In Swift, we can remove first element from the specified array with the help of an in build function named remove(). It removes the first element or we can say element at index 0 from the array. This function only modifies the given array, it does not create a new array. This function remove one element at a time.

Syntax

Following is the syntax −

ArrayName.remove(at: IndexNumber)

Algorithm

Following is the algorithm −

  • Step 1− Declare an array of Int type with values

  • Step 2− Remove the first element of the array using remove() function.

myData.remove(at:0)
  • Step 3− Print the output

Example

The following program shows how to remove first element from the array.

import Foundation import Glibc // Creating array of Int Type var myData = [98, 9, 87, 200, 300] print("Original array: ", myData) // Removing first element from the array // Using remove() function myData.remove(at:0) print("Updated array after removing first element", myData)

Output

Original array: [98, 9, 87, 200, 300]
Updated array after removing first element [9, 87, 200, 300]

Here in the above code, we has an array = [ 98, 9, 87, 200, 300]. Now from this array we remove the first element that is 98 using remove(at:0) function. So the modified array is [ 9 ,87, 200, 300].

Method 3 - Using dropFirst() Function

In Swift, we can also remove first element fro m the specified array with the help of an in build function named dropFirst(). It removes the first element from the array. This function creates a new array to store the result and the original array remains the same. This function remove one element at a time.

Syntax

Following is the syntax −

ArrayName.dropFirst()

Algorithm

Following is the algorithm −

  • Step 1− Declare an array of String type with values

  • Step 2− Remove the first element of the array using dropFirst() function and store the result into a new array.

var output = myData.dropFirst()
  • Step 3− Print the output

Example

The following program shows how to remove first element from the array.

import Foundation import Glibc // Creating an array of String Type var myData = ["apple", "Peach", "mango", "banana"] print("Original array: ", myData) // Removing first element from the array // Using dropFirst() function var output = myData.dropFirst() print("New array after removing first element", output)

Output

Original array: ["apple", "Peach", "mango", "banana"]
New array after removing first element ["Peach", "mango", "banana"]

Here in the above code, we has an array = ["apple" ,"peach" ,"banana"]. Now from this array we remove the first element that is "apple" using myData.dropFirst() function and store the result into new variable. So the output is ["Peach","mango","banana"].

raja
Updated on 10-Oct-2022 13:07:15

Advertisements