Warm-ups · 9 of 24
Linear search
Return the index of the first element equal to target, or -1 if it isn't in the array.
Examples
Input nums = [4, 2, 7, 2], target = 2
Output 1
The first 2 is at index 1.
Input nums = [4, 2, 7], target = 9
Output -1
📘 New to this? Indexing and walking through an array explains the idea in a few minutes.