Last position of
Inputs
values is a list of 0 to 14 whole numbers, each from 0 to 9. target is a whole number from 0 to 9.
Your tasks
- Create a number
foundthat starts at -1. - Use a loop over every position in
values. Whenever the number at that position equalstarget, store the position infound. - Complete
lastIndexOfso it returnsfound. Return it, do not print it.
Example
With values = [3, 1, 4, 1, 5] and target = 1, lastIndexOf returns 3.
Rules
- Positions start at 0.
- If
targetnever appears, return -1.
Test data
TEST DATA 1: values is [3, 1, 4, 1, 5], target is 1
TEST DATA 2: values is [2, 2], target is 7
Practice focus
One pass over a list to add, count, find the biggest and the smallest. Scanning an array once and keeping a running answer is the backbone of linear-time solutions.