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


In this article, we will learn how to write a swift program to get the first given number of items from the array.

An array stores elements of the same data type in order. Therefore, here we use the following methods to get the first given number of items from the array −

  • Using subscript with range

  • Using prefix(_:) function

  • Using prefix(upTo:) function

Method 1: Using subscript with range

To get the first given number of items from the array we can use subscript with different range operators like closed range operator, closed range operator with one-sided ranges, etc.

Algorithm

Step 1 − Create an array with values.

Step 2 − Display original operator.

Step 3 − Using range operators to get the elements from the beginning of the array

Step 4 − Print the modified arrays.

Example

Following Swift program to get 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)

// Getting first 3 elements using closed range operator
let result1 = myArray[0...2]
print("Modified array 1: ", result1)

// Getting first 4 elements using closed range operator with one sided ranges
let result2 = myArray[...3]
print("Modified array 2: ", result2)

// Getting first 2 elements using half open range operator
let result3 = myArray[0..<2]
print("Modified array 3: ", result3)

// Getting first 5 elements using half open range operator with one side ranges
let result4 = myArray[..<5]
print("Modified array 4: ", result4)

Output

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

Here in the above code, we have an array of integer type. Now using various range operators we get the elements from the beginning of the array and display output.

Method 2: Using prefix(_:) function

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

Syntax

func prefix(_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 beginning of the array using prefix() function.

let result1 = myArray.prefix(3)

Step 4 − Print the modified array.

Example

Following Swift program to get the first 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 first 3 elements from the array
let result1 = myArray.prefix(3)
print("Modified array 1: ", result1)

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

Output

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

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

Method 3: Using prefix(upTo:) function

We can also use prefix(upTo:) function to get the first given number of elements from the array. This function will return an array from the start of the given array upto but does not include the specified position.

Syntax

func prefix(upTo y: Self.Index)-> Self.SubSequence

Here, y is the index of 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 beginning of the array using prefix(upTo:) function.

let result1 = myArray.prefix(upTo:2)

Step 4 − Print the modified array.

Example

Following Swift program to get the first 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 first 2 elements from the array
let result1 = myArray.prefix(upTo:2)
print("Modified array 1: ", result1)

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

Output

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

Here in the above code, we have an array of string type. Now we use prefix(upTo: 2) function to get the first 2 elements from the given array and display output. Here this prefix(upTo: 2) function display elements from index 0 to 1 and does not include index 2.

Conclusion

So this is how we can get the first given number of items from the array using any of the following methods.

Updated on: 27-Jan-2023

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements