Search⌘ K
AI Features

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:

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