Continue learning
Practice related DSA questions in the same topic.
Return the first index holding the target, or -1 when it is absent.
Return the first index holding the target, or -1 when it is absent.
def solve(values, target): i = 0 while (i < len(values)): if (values[i] == target): return i i = (i + 1) return -1Inspect position 0 for the target 3.
Inspect position 0 for the target 3.
Check numbered lockers in order until the label matches.
Read each position once, stopping at the first match.
O(n) time · O(1) auxiliary space
Worked example · {"values":[8,3,5,3],"target":3} ⇒ 1
Edge case 1 · {"values":[],"target":4} ⇒ -1
Edge case 2 · {"values":[1],"target":1} ⇒ 0
Stopping one position early misses a match at the very end.
Can any algorithm skip positions in an unsorted collection?