Mental Model
In a sorted array, all occurrences of a number are next to each other. We find where the number starts and ends to count how many times it appears.
Analogy: Imagine a row of identical books on a shelf sorted by title. To count how many copies of one title there are, find the first copy and the last copy, then count all in between.
Array: [1, 2, 2, 2, 3, 4, 5] Indexes: 0 1 2 3 4 5 6 Target: 2 First occurrence at index 1 -> 2 -> 2 -> 2 ← last occurrence at index 3