Element Occurrence

Let's try to count elements. This lesson is of medium-level difficulty.

Problem statement

Suppose we are given a sorted array of integers, find the number of occurrences of a given target value. If the target is not found in the array, return -1.

Constraints:

  • 1nums.length1041 \leq nums.length \leq 10^4
  • 104nums[i]104-10^4 \leq nums[i] \leq 10^4
  • numsnums contains values sorted in an ascending order.
  • 104target104-10^4 \leq target \leq 10^4

Example 1:

Input: [5, 7, 7, 8, 8, 10], 8    
Output: 2

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.