Swift Program to get the last given number of items from the array


In this article, we will learn how to write a swift program to get 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 are using the following methods to get the last given number of items from the array −

  • Using user defined function

  • Using suffix(_:) function

  • Using suffix(from:) function

Method 1: Using a user-defined function

To get the last given number of items from the array we can create a function. This function will return an array containing the last given number of elements.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Calculate start index.

  • Step 3 − Calculate end index.

  • Step 4 − Return a resultant array containing the last elements of the array.

  • Step 5 − Create an array.

  • Step 6 − Call the above function and pass the array and the number of element which we want.

  • Step 7 − Print the output.

Example

Following Swift program to get the last given number of items from the array.

import Foundation
import Glibc

// Function to return the last elements from the array
func lastArrayElements(from arr: [Int], ItemNumber: Int) -> [Int]
{
   let startIndex = max(arr.count - ItemNumber, 0)
   let endIndex = arr.count
   return Array(arr[startIndex..<endIndex])
}

// Original array
let arr = [10, 56, 30, 23, 98, 40, 56, 29]
print("Original Array:", arr)
let resArray = lastArrayElements(from: arr, ItemNumber: 4)
print("Modified Array:", resArray)

Output

Original Array: [10, 56, 30, 23, 98, 40, 56, 29]
Modified Array: [98, 40, 56, 29]

Here in the above code, we have an array of integer type. Now we create a user defined function named lastArrayElements() to get the last 4 elements from the array. In this function, we uses max() function to ensure that if the requested number of items is greater than the length of the array. Then we create a new array by slicing the original array from the calculated startIndex to the end of the array.

Method 2: Using suffix(_:) function

We can also use suffix() function to get the last given number of elements from the array. This function will return an array upto the specified maximum length, containing the last elements of the specified collection.

Syntax

func suffix(_y: Int)

Here, y is the maximum number of elements to return and the value of y must be greater than or equal to 0.

Algorithm

Step 1 − Create an array with values.

Step 2 − Print the original array.

Step 3 − Get the elements from the end of the array using suffix() function.

let result1 = myArray.suffix(3)

Step 4 − Print the modified array.

Example

Following Swift program to get the last given number of items from the array.

import Foundation
import Glibc

// Creating an array of string type
var myArray = ["Cow", "Dog", "Cat", "Bee", "Owl", "Hen"]
print("Original Array:", myArray)

// Getting last 3 elements from the array
let result1 = myArray.suffix(3)
print("Modified array 1: ", result1)

// Getting last 4 elements from the array
let result2 = myArray.suffix(4)
print("Modified array 2: ", result2)

Output

Original Array: ["Cow", "Dog", "Cat", "Bee", "Owl", "Hen"]
Modified array 1: ["Bee", "Owl", "Hen"]
Modified array 2: ["Cat", "Bee", "Owl", "Hen"]

Here in the above code, we have an array of string type. Now we use suffix() function to get the last 3 elements from the given array and display output.

Method 3: Using suffix(from:) function

We can also use suffix(from:) function to get the last given number of elements from the array. This function will return an array from the specified position to the end of the given array.

Syntax

func suffix(from y: Self.Index)-> Self.SubSequence

Here, y is the index from which we add elements in the resulting array. The value of y must be a valid index of the array.

Algorithm

Step 1 − Create an array with values.

Step 2 − Print the original array.

Step 3 − Get the elements from the end of the array using suffix(from:) function.

let result1 = myArray.suffix(from:4)

Step 4 − Print the modified array.

Example

Following Swift program to get the last given number of items from the array.

import Foundation
import Glibc

// Creating an array of string type
var myArray = ["Cow", "Dog", "Cat", "Bee", "Owl", "Hen"]
print("Original Array:", myArray)

// Getting last 2 elements from the array
let result1 = myArray.suffix(from:4)
print("Modified array 1: ", result1)

// Getting last 4 elements from the array
let result2 = myArray.suffix(from: 2)
print("Modified array 2: ", result2)

Output

Original Array: ["Cow", "Dog", "Cat", "Bee", "Owl", "Hen"]
Modified array 1: ["Owl", "Hen"]
Modified array 2: ["Cat", "Bee", "Owl", "Hen"]

Here in the above code, we have an array of string type. Now we use suffix(from: 4) function to get the last 2 elements from the given array and display output. Here, this suffix(from: 4) function display elements from index 4 to the end of the array.

Conclusion

So this is how we can get the last given number of items from the array using any of the following methods. These methods return the result into new array, they does not modified the original array.

Updated on: 27-Jan-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements