Continue learning
Practice related DSA questions in the same topic.
Count all positions that contain the target value.
Count all positions that contain the target value.
def solve(values, target): i = 0 count = 0 while (i < len(values)): if (values[i] == target): count = (count + 1) i = (i + 1) return countTest the next value against 4. The count so far is 0.
Test the next value against 4. The count so far is 0.
A ticket checker makes one tally mark for each passenger on a list.
Keep a count of matches in the prefix already inspected.
O(n) time · O(1) auxiliary space
Worked example · {"values":[4,2,4,4],"target":4} ⇒ 3
Edge case 1 · {"values":[],"target":3} ⇒ 0
Edge case 2 · {"values":[1,1],"target":0} ⇒ 0
Finding the first match does not finish the count.
What changes if the input is sorted?