swift

Find Element Index()

Parameters: array: [Type], element: Type

Array to be searched and the element to locate

Returns: Index of the found element or nil if not found

The Find Element Index function iterates through an array, comparing each element with the given value. The index of the matched element is then returned.

Arrays
Looping
Comparisons
Indexing
Medium dificulty

Creating a Swift Function to Find the Index of an Element in an Array

Welcome to this blog post, fellow programmer! Today, we will delve into how high-level language Swift works when trying to find the index of a specific element in an array. Stick with us as we unravel the process step by step, keeping things simple and universal without any jargon to cloud our understanding. Keep coding, and keep exploring with us!

Step 1: Define the function

Firstly, we need to define our function which we will call findIndex. This function will take two parameters, an array of elements and an element we want to find the index of. At this point the function does nothing.

func findIndex (elements: [Int], targetElement: Int) { 
}

Step 2: Setup loop to go through the array

Inside our function, introduce a for loop to iterate over the array. We will also use a counter to keep track of our current position.

func findIndex (elements: [Int], targetElement: Int) { 

for (index, element) in elements.enumerated() { 
    }
}

Step 3: Element comparison

We will need to check if the element in the current index is equal to the target element. If true, we will return the index.

func findIndex (elements: [Int], targetElement: Int) { 

for (index, element) in elements.enumerated() { 
    if element == targetElement {
        return index 
    }
}
return nil 
}

Step 4: Return nil

If the element is not found in the array, function will return nil indicating that the element doesn't exist in the array.

func findIndex (elements: [Int], targetElement: Int) -> Int? { 

for (index, element) in elements.enumerated() { 
        if element == targetElement {
        return index 
    }
}
return nil 
}

Conclusion

Now, we have a function that will take an array and a target element as inputs and return the index of that element in the array or nil if it doesn't exist.

Here is the full code:

func findIndex (elements: [Int], targetElement: Int) -> Int? { 

for (index, element) in elements.enumerated() { 
    if element == targetElement {
        return index 
    }
}
return nil 
}

Testing this function with some inputs like findIndex(elements: [5,3,9,2], targetElement: 9) should return 2 which is the correct index.

Learn function in:

Array Index Searching

Locating an element's position within an array

Learn more

Mathematical principle

The principle behind the 'find element index' function is simple linear search invoked over the array data type. It is `O(n)` complexity, where `n` is the number of elements in the array. This is because in the worst case the algorithm will have to look at every single item.

Learn more