Challenge: Count Element Occurrence

This problem involves finding the frequency of the occurrence of a given target value in the array.

Problem statement

Given a sorted array of integers, find the frequency of the occurrence of a given target value in the array.
If the value is not found in the array at all, return 0.

Input

A sorted array and a key value to search for.

Output

The number of times thae value occurs in the array.

Sample input

int arr[] = {-5,-3,0,1,3,3,3,4,5};
int s = 3;

Sample output

3

Coding exercise

Take a close look at the challenge and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

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