Solution: Count Element Occurrence
Here is a detailed analysis of the different ways to count the frequency of a number in a sorted array of integers
We'll cover the following...
We'll cover the following...
Solution #1: Brute Force with Linear Search
This is an extremely simple way to solve this problem. We simply initialize a variable to keep count called, count to 0 and then iterate over the array, increasing count by 1 every time the target value is encountered.
Time Complexity
The time complexity of this algorithm is in since the array is iterated over once.
Solution #2: Using Binary Search
This is also a very simple solution. It draws upon the fact that if an element exists in a sorted array, all of its occurrences exist contiguously. It ...