Swift Program to get the subarray from an array using a specified range of indices


In swift, we have various methods like Array() initializer and for-in loop that can be used for getting the subarray from an array using a specified range of indices. An array stores elements of the same data type in an order. Let’s understand both the methods in detail in this article.

Method 1: Using Array() initialiser

To get the subarray from an array using a specified range of indices we uses Array() initialiser. Here we find the subarray using range operator and then convert the elements into the array using Array() initialiser.

Algorithm

  • Step 1 − Create an array.

  • Step 2 − Declare a variable to store the starting index of the range.

  • Step 3 − Declare another variable to store the ending index of the range.

  • Step 4 − Find the subarray using the range operator and then convert the result into the array using Array() initialiser and store the resultant array into a new variable.

  • Step 5 − Print the output.

Example

Following Swift program to get the subarray from an array using a specified range of indices.

import Foundation
import Glibc

// Creating an array of string type
let Balls = ["Blue-2", "Red-4", "Yellow-4", "Green-2", "Pink-5"]

let start = 2
let end = 4

// Getting the subarray
var resArray = Array(Balls[start...end])

print("Resultant Array is:", resArray)

Output

Resultant Array is: ["Yellow-4", "Green-2", "Pink-5"]

Here in the above code, we have an array of string type. Then create two variable to store the starting and the ending index of the range that are start = 2 and end = 4. Then find the subarray using the range operator and then convert the result into array and display output.

Method 2: Using for-in Loop

In this method, we use for-in loop to find a subarray from the given array using a specified range of indices.

Algorithm

  • Step 1 − Create an array.

  • Step 2 − Declare another variable to store the character which will insert in-between the each elements of the array.

  • Step 3 − Create an empty array to store the resultant array.

  • Step 4 − Run a for-in loop with the specified range and append the elements in the new array.

  • Step 5 − Print the output.

Example

Following Swift program to get the subarray from an array using a specified range of indices.

import Foundation
import Glibc

// Creating an array of integer type
let array = [4, 5, 3, 2, 8, 1, 99, 2, 33, 66, 11]
let startRange = 4
let endRange = 7

// Empty array
var resArray = [Int]()

// Getting the subarray
for indi in startRange...endRange {
   resArray.append(array[indi])
}
print("Resultant Array is:", resArray)

Output

Resultant Array is: [8, 1, 99, 2]

Here in the above code, we have an array of integer type. Then create two variable to store the starting and the ending index of the range that are start = 4 and end = 7. Then find the subarray using the for in loop and then append the result into the new array and display final output.

Conclusion

So this is how we can get the subarray from an array using a specified range of indices. Here both the methods uses starting and ending index value along with the range operator to find the subarray.

Updated on: 19-Jul-2023

813 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements