Swift Program to remove the first given number of items from the array


In this article, we will learn how to write a swift program to remove the first 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 first given number of items from the array −

  • Using removeFirst(_:) function

  • Using dropFirst(_:) function

  • Using removeSubrange() function

Method 1: Using removeFirst(_:) Function

Swift provide an in-built function named as removeFirst() function to remove the specified number of items from the start of the array.

Syntax

func removeFirst(_x: Int)

Here, x is the total number of elements which we wants to remove from the beginning 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 from the array.

Step 3 − Remove the first elements from the given array using removeFirst() function.

myArray.removeFirst(x)

Step 4 − Print the modified array.

Example

Following Swift program to remove the first given number of items from the array.

import Foundation
import Glibc

// Creating an array of integer type
var myArray = [84, 2, 3, 5, 7, 81, 22, 23, 11, 12, 22, 23, 4]
print("Original Array:", myArray)
let x = 5

// Removing first elements
myArray.removeFirst(x)
print("Modified Array:", myArray) 

Output

Original Array: [84, 2, 3, 5, 7, 81, 22, 23, 11, 12, 22, 23, 4]
Modified Array: [81, 22, 23, 11, 12, 22, 23, 4]

Here in the above code, we have an array of integer type. Now using removeFirst() function we remove the first five elements from the given array and display the modified array.

Method 2: Using dropFirst(_:) function

We can also use dropFirst() function to remove the first 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 dropFirst(_y: Int)

Here, y is the total number of elements which we wants to remove from the beginning 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 start of the array

Step 3 − Remove the first elements from the given array using dropFirst() function.

myArray.dropFirst(x)

Step 4 − Print the modified array.

Example

Following Swift program to remove the first 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 = 4

// Removing first elements
var result = myArray.dropFirst(x)
print("Modified Array:", result) 

Output

Original Array: [73, 23, 1, 34, 5, 6, 7, 8, 9, 10, 49]
Modified Array: [5, 6, 7, 8, 9, 10, 49]

Here in the above code, we have an array of integer type. Now we use dropFirst() function to remove first 4 elements from the given array and display the output.

Method 3: Using removeSubrange(_:) function

To remove the first 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 first given number of elements from the array using removeSubrange() function.

Step 4 − Print the output.

Example

Following Swift program to remove the first 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 = 3

// Removing first elements
Number.removeSubrange(0..<x)
print("Modified Array:", Number)

Output

Original Array: [4, 56, 88, 34, 2, 1, 13, 25, 56, 11]
Modified Array: [34, 2, 1, 13, 25, 56, 11]

Here in the above code, we have an array of integer type. Now we pass a range 0 to n-1 in the removeSubrange() function to remove first 3 elements from the given array and display the output.

Conclusion

So this is how we can remove the first given number of elements from the array. Here, removeFirst(_:) and removeSubrange() functions modify the original array but dropFirst() 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 dropFirst() function to remove first given number of elements from the array.

Updated on: 27-Jan-2023

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements