# Swift program to get the first n elements from an array

SwiftServer Side ProgrammingProgramming

#### Swift 4 Online Training

38 Lectures 1 hours

#### Swift Basics: Learn to Code from Scratch [For Beginners]

13 Lectures 2 hours

#### How to create Tic-Tac-Toe Game for iPhone using Swift in Xcode - iOS App

7 Lectures 1 hours

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

In an array, we can find the first n elements very easily using any of the following method.

Below is a demonstration of the same −

Input

Suppose our given input is −

myArray = [ 34, 35, 56, 66, 99, 22]
Number = 3


Output

The desired output would be −

First 3 elements = [ 34, 35, 56]


## Method 1 - Using half opened range operator with one-side ranges

In Swift, we can get the first n elements from the specified array using half opened range operator with one side range that is [..<y] As we know that a range starts from x to y, so this range starts from x but does not contain y. Or we can say that this range include first values but doesn’t contain end value.

### Syntax

Following is the syntax −

var res = myArr[..<number]


### Algorithm

Following is the algorithm −

• Step 1− Declare an array with value.

• Step 2− Getting the first four or n elements from an array using half op ened operator with one side range.

var result = myArray[..<4]

• Step 3− Print the output.

### Example

The following program shows how to get the first n elements from an array.

import Foundation
import Glibc

var myArray = [23, 87, 97, 37, 29, 383, 32, 2, 1]

// Getting the first 4 elements from the array
// Using one sided half-open range operator
var result = myArray[..<4]
print("The array =",myArray)

print("The first 4 elements of an array", result)


### Output

The array = [23, 87, 97, 37, 29, 383, 32, 2, 1]
The first 4 elements of an array [23, 87, 97, 37]


Here in the above code, we have an array [ 87, 97, 37, 29, 383, 32, 2, 1]. Now we display the first 4 elements from the array using half open range operator

var result = myArray[..<4]


This operator will r eturn the first four elements from the array that are − [ 23 , 87, 97, 37]

## Method 2 - Using prefix() function

Swift provide an in built function named prefix to find the first n elements from the specified array. It takes one argument that is the required number of elements.

### Syntax

Following is the syntax −

var res = myArr.prefix(number)


### Algorithm

Following is the algorithm −

• Step 1− Declare an array with value.

• Step 2− Getting the first two or n elements from an array using prefix().

var result = myArray.prefix(2)

• Step 3− Print the output.

### Example

The following program shows how to get the first n elements from an array.

import Foundation
import Glibc

var myArray = ["Bus", "Car", "Bike", "Boat", "Bicycle"]

// Getting the first 2 elements from the array
// Using prefix() function

var result = myArray.prefix(2)
print("The array =",myArray)
print("The first 2 elements of an array:", result)


### Output

The array = ["Bus", "Car", "Bike", "Boat", "Bicycle"]
The first 2 elements of an array: ["Bus", "Car"]


Here in th e above code, we have an array ["Bus", "Car", "Bike", "Boat", "Bicycle"] Now we display the first 2 elements from the array using prefix() function

var result = myArray.prefix(2)


This operator will return the first two elements from the array that are: ["Bus","Car"]

Updated on 10-Oct-2022 13:02:56