What if you could count repeated items in a huge sorted list in just a few quick steps?
Why Count Occurrences of Element in Sorted Array in DSA C#?
Imagine you have a long list of sorted numbers, like a phone book sorted by last name. You want to find out how many times a particular name appears. Doing this by checking each entry one by one would take a lot of time.
Manually scanning the entire sorted list to count occurrences is slow and tiring. It wastes time because you look at every item, even when the list is very long. It's easy to make mistakes by losing track or missing some entries.
Using a smart method that takes advantage of the sorted order, you can quickly find where the target element starts and ends. This way, you count occurrences without checking every item, saving time and avoiding errors.
int count = 0; for (int i = 0; i < array.Length; i++) { if (array[i] == target) count++; }
int first = FindFirstOccurrence(array, target); int last = FindLastOccurrence(array, target); int count = (first == -1) ? 0 : last - first + 1;
This method lets you count how many times an element appears in a sorted list very fast, even if the list is huge.
When searching a sorted database of customer orders, you can quickly find how many orders were placed by a specific customer without scanning all records.
Manual counting is slow and error-prone for large sorted lists.
Using the sorted order, we find first and last positions to count efficiently.
This saves time and reduces mistakes when counting occurrences.
Practice
Solution
Step 1: Understand binary search efficiency
Binary search divides the search space in half each time, making it faster than checking every element.Step 2: Compare with linear scan
Linear scan checks each element, taking O(n) time, while binary search takes O(log n) time.Final Answer:
It reduces the time complexity to O(log n) compared to a linear scan. -> Option CQuick Check:
Binary search = O(log n) speed [OK]
- Thinking binary search works on unsorted arrays
- Confusing time complexity with memory usage
- Assuming binary search counts without element checks
Solution
Step 1: Identify return type for count
The function should return an integer representing the count, so return type must be int.Step 2: Check parameter order and types
Common convention is array first, then target element; parameters types must match int array and int target.Final Answer:
int CountOccurrences(int[] arr, int target) -> Option BQuick Check:
Return count as int, array then target [OK]
- Using void return type instead of int
- Swapping parameter order incorrectly
- Returning bool instead of count
int[] arr = {1, 2, 2, 2, 3, 4, 5};
int target = 2;
int count = CountOccurrences(arr, target);
Console.WriteLine(count);
int CountOccurrences(int[] nums, int x) {
int first = Array.BinarySearch(nums, x);
if (first < 0) return 0;
int count = 1;
int left = first - 1;
while (left >= 0 && nums[left] == x) {
count++;
left--;
}
int right = first + 1;
while (right < nums.Length && nums[right] == x) {
count++;
right++;
}
return count;
}Solution
Step 1: Find first occurrence index using BinarySearch
BinarySearch finds one occurrence of 2 at index 2.Step 2: Count occurrences left and right of index 2
Left side: index 1 and 0 checked; index 1 is 2 (count=2), index 0 is 1 (stop). Right side: index 3 is 2 (count=3), index 4 is 3 (stop).Final Answer:
3 -> Option AQuick Check:
Occurrences of 2 = 3 [OK]
- Assuming BinarySearch returns first occurrence always
- Counting only one side neighbors
- Returning 0 if not found without checking neighbors
int CountOccurrences(int[] nums, int target) {
int first = Array.BinarySearch(nums, target);
if (first < 0) return 0;
int count = 1;
int left = first - 1;
while (left > 0 && nums[left] == target) {
count++;
left--;
}
int right = first + 1;
while (right < nums.Length && nums[right] == target) {
count++;
right++;
}
return count;
}Solution
Step 1: Analyze left boundary condition in while loop
The loop checks 'left > 0', so it skips index 0, missing a possible occurrence at start.Step 2: Correct boundary condition to include index 0
Changing condition to 'left >= 0' ensures index 0 is checked for target equality.Final Answer:
The left boundary check should be 'left >= 0' instead of 'left > 0'. -> Option DQuick Check:
Check array start index 0 with '>= 0' [OK]
- Using 'left > 0' misses first element
- Returning -1 instead of 0 when not found
- Starting count at 0 instead of 1
- Using wrong right boundary condition
Solution
Step 1: Use binary search to find first occurrence of target
Modify binary search to find the lowest index where target appears.Step 2: Use binary search to find last occurrence of target
Modify binary search to find the highest index where target appears.Step 3: Calculate count from indices
Count = last occurrence index - first occurrence index + 1.Final Answer:
Find the first and last occurrence indices of the target using binary search, then calculate count as last - first + 1. -> Option AQuick Check:
Count = last - first + 1 using two binary searches [OK]
- Using linear scan after one binary search
- Sorting array again unnecessarily
- Using hash map instead of binary search
Ready to prove it?
Pass the test and this pattern moves unknown
