Index of target
Warm-up · Write code · about 5 min
Given an array of integers nums in any order and a target, return the index of the first cell equal to target. Return -1 if no cell holds it.
Example 1
Input:
[[7,3,9,1,8,4,6,2],8]
Output:
4
Example 2
Input:
[[7,4,9,4,1],4]
Output:
1
4 appears twice. Return the first index.
Constraints
- 0 ≤ nums.length ≤ 10,000
- -10,000 ≤ nums[i], target ≤ 10,000
- nums may contain repeats
Learn the idea first: Linear search, a visual lesson.
More Linear search problems
- Count occurrences Standard
- Last index of target Standard
- Trace a linear scan Standard
- Search with a sentinel Challenge