swift Array enumerated() Function



The enumerated() function is used to get a pair(m, n) from the given array, where m represents the index of the element and n represents the element associated with that index. It is generally used for zero-based indexed collections like arrays, etc.

For example, we have an array = [4, 9, 2, 5, 19, 11]. Now using the max() function we will find the maximum element that is 19.

Syntax

Following is the syntax of the enumerated() function −

func enumeration()

Return value

This function returns a pair containing the element and its associated index value.

Now we will discuss the use of the enumerated() function with the help of the following examples:

Example 1

Swift program to iterate through the given array and find the index of the given elements using the enumerated() function.

import Foundation

// Input array of integer type
var arr1 = [23, 56, 77, 12, 1, 19, 10, 11, 13]

// Iterating through each element using enumerated() function
for (index, element) in arr1.enumerated(){
    print("Index:\(index) Element:\(element)")
}

Output

Index:0 Element:23
Index:1 Element:56
Index:2 Element:77
Index:3 Element:12
Index:4 Element:1
Index:5 Element:19
Index:6 Element:10
Index:7 Element:11
Index:8 Element:13

Example 2

Swift program to determine the index of each element using the enumerated () function.

import Foundation

// Input array of string type
var names = ["Bike", "Car", "Truck", "Boat"]

// Iterating through each element using the enumerated() function
for (index, str) in names.enumerated(){
    print("String:\(str) Index:\(index)")
}

Output

String:Bike Index:0
String:Car Index:1
String:Truck Index:2
String:Boat Index:3

Example 3

Swift program to iterate through each element of the given array and then find the maximum mark scored by the student in the current test along with its index value with the help of max() and enumerated() functions.

import Foundation

// Marks of all the subjects
let marks = [60, 45, 68, 66, 78, 98]

// Find the maximum mark and its index with the help of the enumerated() function
if let (index, mark) = marks.enumerated().max(by: { $0.element < $1.element }) {
    print("Maximum Marks: \(mark) at Index: \(index)")
} else {
    print("Given array is empty")
}

Output

Maximum Marks: 98 at Index: 5

Example 4

Swift program to find the sum of the elements present at even indices. Here we first find all the even indices using the enumerated() function and filter() function, then using the map() function we map all the elements present at the associated indices into an array. After that using the reduce() function we calculate the sum of these elements.

import Foundation

// Input array
let numbers = [10, 12, 7, 3, 18, 2]

// Finding the even indices
let evenValues = numbers.enumerated().filter { $0.offset % 2 == 0 }.map { $0.element }

// Calculating the sum of numbers present at even indices
let result = evenValues.reduce(0, +)

print("Sum of elements at even indices: \(result)")

Output

Sum of elements at even indices: 35
Advertisements