Problem overview
The method find receives an array of integers nums and an integer value. It returns the index of value in the array, or -1 if the value isn't there. For Array(1, 2, 4, 5) and value = 4, the 4 sits at index 2, so the answer is 2.
The statement asks for O(n) time and O(1) extra space. The array isn't sorted, so you can't skip any element in advance.
Approach: check each position in order
This is linear search. Start at the first element and check one position after another until you either find the value or run out of array. The first position where the element equals value is the answer, and you can stop right there.
Looping over the elements, as in for (num <- nums), is not enough, because the loop variable holds a value and not its position. To return an index, loop over the indices and read the element at each one.
If the loop ends without a match, then every position was checked and none held the value, so the method returns -1. That number can never be a real index, so the caller can't confuse it with a position.
Step by step
- Loop over the indices with
for (i <- nums.indices). The rangenums.indicesis0 until nums.length, every valid position. - Test
nums(i) == value. Scala reads an element with parentheses. If they are equal, returniimmediately. Returning inside the loop stops the search at the first match, so when the value occurs several times, you get its first index. - Return -1 after the loop. The line is reached only if no
returnran inside the loop.
The solution in Scala
class Solution {
def find(nums: Array[Int], value: Int): Int = {
for (i <- nums.indices) {
if (nums(i) == value) {
return i
}
}
return -1
}
}
For nums = Array(1, 2, 4, 5) and value = 4, the loop checks index 0 (1), index 1 (2) and index 2 (4). The third check succeeds, so the method returns 2 and never looks at index 3.
Language details
returninside aforloop. A Scalaforloop over a range is compiled to a call that takes the body as a function, and areturninside it leaves the whole method. This works as the lesson needs. Scala 3 prints a compiler warning that such non-local returns are no longer supported and suggestsscala.util.boundary, but the code compiles and returns the right index.- The result type. The method's type is
Int, so both the index and -1 fit.
Complexity
In the worst case the value is the last element or missing, so the loop checks all n positions: O(n) time. When the value is early in the array the method returns sooner, but the bound is stated for the worst case. The method stores only the loop variable, so the extra space is O(1).
Edge cases and common mistakes
- The value is missing. The loop ends without returning and the method returns -1. With
Array(1, 2, 4, 5)andvalue = 9the answer is -1. - Duplicates.
Array(4, 4)withvalue = 4returns 0, the first index. - An empty array. The loop doesn't run and the method returns -1.
- Putting
return -1inside the loop. The method would give up after the first element that doesn't match. The -1 belongs after the loop. - Returning the value instead of the index.
return nums(i)hands back the element you already knew. The task asks fori. - Square brackets.
nums[i]is not how Scala reads an array element. Usenums(i).
Testing
Try Array(1, 2, 4, 5) with 4 (2), the same array with 9 (-1), Array(4, 4) with 4 (0), and an empty array with any value (-1).
