Mental Model
In a sorted list, all copies of the same 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. To count how many copies of one book are there, find the first copy and the last copy, then count all books in between.
Sorted array: [1, 2, 2, 2, 3, 4, 5]
Find element 2:
Index: 0 1 2 3 4 5 6
Values: 1-> 2-> 2-> 2-> 3-> 4-> 5
↑ ↑
first 2 last 2