
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
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