0
0
DSA C++programming~15 mins

Count Occurrences of Element in Sorted Array in DSA C++ - Deep Dive

Choose your learning style9 modes available
Overview - Count Occurrences of Element in Sorted Array
What is it?
Counting occurrences of an element in a sorted array means finding how many times that element appears in the array. Since the array is sorted, all occurrences of the element are grouped together. This allows us to find the count efficiently without checking every element. The goal is to quickly find the first and last positions of the element and calculate how many times it appears.
Why it matters
Without this method, counting occurrences would require checking every element, which is slow for large arrays. Efficient counting helps in searching, statistics, and data analysis where quick answers are needed. For example, in a sorted list of customer IDs, knowing how many times a specific ID appears can help detect duplicates or frequency. Without this, programs would be slower and less responsive.
Where it fits
Before this, learners should understand arrays and basic searching techniques like linear and binary search. After this, learners can explore related topics like searching in rotated arrays, frequency counting in unsorted arrays, and advanced searching algorithms.
Mental Model
Core Idea
In a sorted array, the count of an element equals the difference between the positions of its last and first occurrence plus one.
Think of it like...
Imagine a row of identical books on a shelf sorted by title. To count how many copies of one title you have, you find the first copy and the last copy on the shelf, then count all books between them.
Sorted Array: [1, 2, 2, 2, 3, 4, 5]
Element to count: 2
Positions: First occurrence at index 1, Last occurrence at index 3
Count = 3 - 1 + 1 = 3
Build-Up - 7 Steps
1
FoundationUnderstanding Sorted Arrays
🤔
Concept: Learn what a sorted array is and why elements are grouped.
A sorted array is a list of elements arranged in increasing or decreasing order. Because of this order, all equal elements appear next to each other. For example, in [1, 2, 2, 3, 4], the two '2's are side by side. This grouping helps us find elements faster than in an unsorted list.
Result
You understand that equal elements are clustered, which is key to efficient counting.
Knowing that equal elements are together allows us to use special search methods instead of checking every element.
2
FoundationBasic Linear Counting Method
🤔
Concept: Count occurrences by checking each element one by one.
Start from the beginning of the array and check each element. Increase a counter when the element matches the target. Stop when you reach the end. This method works but is slow for large arrays because it looks at every element.
Result
For array [1, 2, 2, 2, 3], counting '2' gives 3 after checking all elements.
This method is simple but inefficient for big data; it sets the stage for faster methods.
3
IntermediateUsing Binary Search to Find First Occurrence
🤔Before reading on: do you think binary search can find the first occurrence by stopping at the first match or do we need extra steps? Commit to your answer.
Concept: Modify binary search to find the first position where the element appears.
Binary search normally finds any occurrence of the element. To find the first occurrence, when you find the element, continue searching to the left half to see if it appears earlier. Keep track of the current match and move left until no earlier match is found.
Result
For [1, 2, 2, 2, 3], first occurrence of '2' is at index 1.
Knowing how to adjust binary search to find boundaries is key to efficient counting.
4
IntermediateUsing Binary Search to Find Last Occurrence
🤔Before reading on: do you think finding the last occurrence is similar to the first but searching right? Commit to your answer.
Concept: Modify binary search to find the last position where the element appears.
When binary search finds the element, continue searching to the right half to find if it appears later. Keep track of the current match and move right until no later match is found.
Result
For [1, 2, 2, 2, 3], last occurrence of '2' is at index 3.
This symmetrical approach to first occurrence lets us find exact boundaries efficiently.
5
IntermediateCalculating Count from Boundaries
🤔
Concept: Use the first and last occurrence indices to find the total count.
Once you have the first and last positions of the element, subtract the first index from the last and add one. This gives the total number of times the element appears.
Result
Count = last_index - first_index + 1 = 3 - 1 + 1 = 3 for element '2'.
This simple formula leverages the sorted property to count without scanning all elements.
6
AdvancedComplete Efficient Counting Algorithm
🤔Before reading on: do you think combining two binary searches is faster than linear counting? Commit to your answer.
Concept: Combine first and last occurrence binary searches to count occurrences in O(log n) time.
Implement two binary search functions: one for first occurrence and one for last occurrence. Call both and calculate count. This method is much faster than linear counting, especially for large arrays.
Result
For array [1, 2, 2, 2, 3, 4], counting '2' returns 3 quickly.
Combining two boundary searches exploits sorting to achieve fast counting.
7
ExpertHandling Edge Cases and No Occurrence
🤔Before reading on: if the element is not in the array, do you think binary search returns -1 or something else? Commit to your answer.
Concept: Adjust the algorithm to handle cases where the element does not exist in the array.
If binary search does not find the element, it returns -1 or an invalid index. The counting function should check for this and return zero. This prevents wrong counts and errors.
Result
Counting '5' in [1, 2, 2, 3] returns 0 correctly.
Properly handling no-match cases ensures robustness in real-world applications.
Under the Hood
Binary search works by repeatedly dividing the search interval in half. For first occurrence, when a match is found, the search continues in the left half to find an earlier match. For last occurrence, it continues in the right half. This process uses the sorted order to eliminate half the array each step, making it very fast. The count is then derived from these boundary indices.
Why designed this way?
This approach was designed to improve search speed from linear O(n) to logarithmic O(log n). Sorting the array groups equal elements, enabling boundary searches. Alternatives like linear search are simpler but slower. The binary search method balances speed and simplicity, making it a standard technique.
Array indices:  0   1   2   3   4   5
Array values:  [1,  2,  2,  2,  3,  4]

Binary Search for first occurrence:
Start low=0, high=5
mid=2 -> value=2 (match)
Move high=mid-1=1 to find earlier
mid=0 -> value=1 (less)
Move low=mid+1=1
mid=1 -> value=2 (match)
Move high=mid-1=0
Stop, first occurrence at index 1

Binary Search for last occurrence:
Start low=0, high=5
mid=2 -> value=2 (match)
Move low=mid+1=3 to find later
mid=4 -> value=3 (greater)
Move high=mid-1=3
mid=3 -> value=2 (match)
Move low=mid+1=4
Stop, last occurrence at index 3
Myth Busters - 4 Common Misconceptions
Quick: Does binary search always find the first occurrence of an element? Commit yes or no.
Common Belief:Binary search returns the first occurrence of the element automatically.
Tap to reveal reality
Reality:Standard binary search returns any occurrence, not necessarily the first.
Why it matters:Assuming it returns the first can cause wrong counts and bugs in programs.
Quick: If the element is not in the array, does binary search return -1 or crash? Commit your answer.
Common Belief:Binary search always finds the element or returns -1 safely.
Tap to reveal reality
Reality:Binary search returns -1 or invalid index if not found, but code must check this explicitly.
Why it matters:Ignoring this can cause incorrect counts or runtime errors.
Quick: Is linear counting faster than binary search for large sorted arrays? Commit yes or no.
Common Belief:Checking each element one by one is as fast as binary search for counting.
Tap to reveal reality
Reality:Linear counting is slower (O(n)) compared to binary search (O(log n)) for large arrays.
Why it matters:Using linear counting on big data causes slow programs and poor user experience.
Quick: Does the count equal last occurrence minus first occurrence without adding one? Commit yes or no.
Common Belief:Count = last index - first index is enough.
Tap to reveal reality
Reality:Count = last index - first index + 1 is correct because indices are zero-based and inclusive.
Why it matters:Missing the +1 leads to off-by-one errors and wrong counts.
Expert Zone
1
Binary search for boundaries must carefully update low and high pointers to avoid infinite loops.
2
When elements are repeated many times, boundary binary searches still run in O(log n), making them scalable.
3
In some languages or libraries, built-in functions exist for lower_bound and upper_bound which implement these searches efficiently.
When NOT to use
If the array is unsorted, this method fails. Instead, use hash maps or frequency arrays for counting. Also, if the array is small, linear counting might be simpler and fast enough.
Production Patterns
In databases and search engines, counting occurrences quickly helps in query optimization. Also used in analytics to find frequency of events. Implementations often use built-in binary search utilities or write custom boundary searches for performance.
Connections
Binary Search
Builds-on
Understanding how to modify binary search to find boundaries deepens mastery of this fundamental algorithm.
Frequency Counting with Hash Maps
Alternative approach
Knowing when to use binary search counting versus hash maps helps choose the best tool based on data order and size.
Statistical Mode Calculation
Application
Counting occurrences efficiently is a key step in finding the mode (most frequent value) in data analysis.
Common Pitfalls
#1Off-by-one error in count calculation
Wrong approach:count = last_index - first_index
Correct approach:count = last_index - first_index + 1
Root cause:Forgetting that indices are zero-based and inclusive leads to missing one occurrence.
#2Using standard binary search without boundary checks
Wrong approach:int binarySearch(int arr[], int n, int x) { int low=0, high=n-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==x) return mid; else if(arr[mid]
Correct approach:int firstOccurrence(int arr[], int n, int x) { int low=0, high=n-1, result=-1; while(low<=high) { int mid=(low+high)/2; if(arr[mid]==x) { result=mid; high=mid-1; } else if(arr[mid]
Root cause:Not adjusting search boundaries to find first or last occurrence causes incorrect results.
#3Not handling element not found case
Wrong approach:int countOccurrences(int arr[], int n, int x) { int first = firstOccurrence(arr, n, x); int last = lastOccurrence(arr, n, x); return last - first + 1; }
Correct approach:int countOccurrences(int arr[], int n, int x) { int first = firstOccurrence(arr, n, x); if(first == -1) return 0; int last = lastOccurrence(arr, n, x); return last - first + 1; }
Root cause:Assuming element always exists leads to wrong counts or errors.
Key Takeaways
Sorted arrays group equal elements together, enabling efficient counting.
Binary search can be modified to find the first and last occurrence of an element.
Counting occurrences equals last occurrence index minus first occurrence index plus one.
Handling cases where the element is not found is essential for correctness.
This method runs in O(log n) time, much faster than checking every element.