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