findFirstIndex
Given an array and a target element, return the index of the first occurrence of the target element. If the element is not found, return -1.
Examples
findFirstIndex([1, 2, 3, 4, 3], 3) → 2
findFirstIndex(['a', 'b', 'c'], 'd') → -1
findFirstIndex([5, 10, 15, 20], 5) → 0
Starter code
function findFirstIndex(arr, target) {
}
Complexity of the optimal solution
Time O(n), space O(1). In the worst-case scenario, the function needs to iterate through the entire array to find the element or determine it's not present. This results in a time complexity proportional to the array size (n). The space used is constant.