Count Occurrences in Sorted Array - aloalgo

Count Occurrences in Sorted Array

Medium

You are given a sorted array of integers nums that may contain duplicates, and a target integer target, find the number of occurrences of the target in the array.

Your solution should be more efficient than a linear scan.

Example 1

Inputs
nums = [2, 5, 5, 5, 6, 8, 9]
target = 5
Output
3
Explanation:

The number 5 appears 3 times in the array.

Example 2

Inputs
nums = [1, 2, 3, 5, 6]
target = 4
Output
0
Explanation:

The number 4 is not present in the array.

Example 3

Inputs
nums = [10]
target = 10
Output
1
Explanation:

The number 10 appears once.

Loading...
Inputs
nums = [2, 5, 5, 5, 6, 8, 9]
target = 5
Output
3

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!