
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Index of First Occurrence of Item in Array in Swift
An array is used to store elements of the same data type in order.
Here we will learn how to write a swift program to find the index of the first occurrence of the specified item in the array.
To achieve that we are going to use the following methods to find the index of the first occurrence of the array element ?
Using user-defined function
Using firstIndex() function
Method 1: Using user-defined function
To find the index of the first occurrence of the specified element in the array we create a function which iterate through each elements and their indexes and check if the current value is equal to the specified element. If yes, then it return that current index. If the loop ends and the element does not fount, then it return nil.
Algorithm
Step 1 ? Create a function.
Step 2 ? Run a for loop.
Step 3 ? Check if the current element is equal to specified element. If yes then return the current index.
Step 4 ? Return nil if the loop ends and the specified element not found.
Step 5 ? Create an array and pass it to the function along with the element.
Step 6 ? Print the result
Example
Following Swift program to find the index of the first occurrence of the specified item in the array.
import Foundation import Glibc // Function to find the index of the first occurrence of the specified item in the array func findFirstIndex(ele: Int, in arr: [Int]) -> Int? { for (index, value) in arr.enumerated() { if value == ele { return index } } return nil } // Creating an array of integer type let numbers = [12, 9, 3, 6, 88, 23, 4, 6, 4, 23, 6] if let index = findFirstIndex(ele: 6, in: numbers) { print("The first occurrence of 6 is at index \(index)") } else { print("Element 6 is not found in the give array") }
Output
The first occurrence of 6 is at index 3
Here in the above code, we have an array of integer type. Now we create a function named findFirstIndex() and pass the element and the array in it. In this function, we run a for loop which iterates through each element of the original, and using enumerated() function we get the index and the element. Inside the for loop, we check if the current element is equal to the specified element. If the current element is equal to the specified element, then this function return the current index. If the loop ends and no element is equal to the specified element, then this function returns nil.
Method 2: Using firstIndex(of:) function
Swift provides an inbuilt function named as firstIndex(of:) function. This function returns the first index where the specified elements appear in the given array. If the specified element is not found in the given array, then it will return nil.
Syntax
Func firstIndex(of: Element)
Here, Element represent the item to search for in the array.
Algorithm
Step 1 ? Create an array of integer type.
Step 2 ? Use if-else to check the index of the first occurrence of the specified element in the array using firstIndex(of:) function.
Step 3 ? If the element found in the given array, then display the index value.
Step 4 ? If the element does not found, then print "Element is not found".
Example
Following Swift program to find the index of the first occurrence of the specified item in the array.
import Foundation import Glibc // Creating an array of integer type let mArray = [20, 3, 3, 4, 21, 4, 7, 10, 8, 4, 2] // Finding the index of the first occurrence of the specified item in the array if let index = mArray.firstIndex(of: 4) { print("The first occurrence of 4 is at index \(index)") } else { print("Element 4 is not found in the given array") }
Output
The first occurrence of 4 is at index 3
Here in the above code, we have an array of integer. Then we uses firstIndex(of:) function to find the index of the first occurrence of the element 4 in the array. If the number is found in the array, then display the element along with its index value. Otherwise display element not found. Here we use if let with the firstIndex(of:) function because this function return optional Int value so unwrap to using if let.
Conclusion
In this article we have used two different methods to find the index of the first occurrence of the specified item in the array. In the first method, we have used a user-defined function, whereas in the second example, we have used ian nternal function - firstIndex(of:) to find the index.