📖 Scenario: You are working with a sorted list of numbers representing daily temperatures recorded in a city. You want to find out how many times a specific temperature was recorded.
🎯 Goal: Build a program that counts how many times a given number appears in a sorted array.
📋 What You'll Learn
Create an array called temperatures with the exact values: 10, 12, 12, 15, 15, 15, 20, 20, 25
Create an integer variable called target and set it to 15
Write a function called countOccurrences that takes the array, its size, and the target number, and returns how many times the target appears
Print the count of occurrences using printf
💡 Why This Matters
🌍 Real World
Counting how many times a specific value appears in sorted data is useful in statistics, data analysis, and reporting.
💼 Career
This skill helps in roles like data analyst, software developer, and quality assurance where data processing is common.
Progress0 / 4 steps
1
Create the sorted array
Create an integer array called temperatures with these exact values: 10, 12, 12, 15, 15, 15, 20, 20, 25
DSA C
Hint
Use int temperatures[] = { ... }; to create the array with the exact numbers.
2
Set the target number
Create an integer variable called target and set it to 15
DSA C
Hint
Use int target = 15; to create the variable.
3
Write the function to count occurrences
Write a function called countOccurrences that takes three parameters: an integer array called arr, an integer size, and an integer target. The function should return the number of times target appears in arr. Use a for loop with variable i to check each element.
DSA C
Hint
Use a for loop to check each element and increase count when you find target.
4
Print the count of occurrences
Call the function countOccurrences with temperatures, its size 9, and target. Store the result in an integer variable called count. Then print count using printf with the format "%d\n".
DSA C
Hint
Call the function and print the result with printf("%d\n", count);.
Practice
(1/5)
1. What is the main advantage of using a binary search approach to count occurrences of an element in a sorted array?
easy
A. It uses less memory than linear search
B. It sorts the array automatically
C. It reduces the search time from linear to logarithmic
D. It works only on unsorted arrays
Solution
Step 1: Understand binary search efficiency
Binary search divides the search space in half each time, reducing time complexity to O(log n).
Step 2: Compare with linear search
Linear search checks each element one by one, taking O(n) time, which is slower for large arrays.
Final Answer:
It reduces the search time from linear to logarithmic -> Option C
Quick Check:
Binary search = faster search [OK]
Hint: Binary search halves search space each step [OK]
Common Mistakes:
Thinking binary search sorts the array
Confusing memory usage with speed
Assuming binary search works on unsorted arrays
2. Which of the following C code snippets correctly declares a function to count occurrences of an integer in a sorted array?
easy
A. void countOccurrences(int arr[], int n, int x);
B. int countOccurrences(int arr[], int n, int x);
C. int countOccurrences(int arr, int n, int x);
D. int countOccurrences(int arr[], int x);
Solution
Step 1: Check function return type and parameters
The function should return an int (count), take array, size, and target element as parameters.
Step 2: Validate parameter types
Array parameter must be int arr[] or int *arr, size as int n, and target as int x.
Final Answer:
int countOccurrences(int arr[], int n, int x); -> Option B
Quick Check:
Correct function signature = int countOccurrences(int arr[], int n, int x); [OK]
Hint: Function returns count as int, array passed as int[] [OK]
Common Mistakes:
Using void return type instead of int
Passing array as int instead of int[]
Missing array size parameter
3. What will be the output of the following C code snippet?
int arr[] = {1, 2, 2, 2, 3, 4, 5};
int n = 7;
int x = 2;
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == x) count++;
}
printf("%d", count);
medium
A. 1
B. 2
C. 4
D. 3
Solution
Step 1: Iterate through the array and count matches
Elements equal to 2 are at indices 1, 2, and 3, so count increments 3 times.
Step 2: Print the count
The final count is 3, so printf outputs 3.
Final Answer:
3 -> Option D
Quick Check:
Count of 2 in array = 3 [OK]
Hint: Count elements equal to x by scanning array once [OK]
Common Mistakes:
Counting only first occurrence
Off-by-one errors in loop
Printing count before loop ends
4. Identify the error in the following C function intended to count occurrences of x in a sorted array using binary search:
int countOccurrences(int arr[], int n, int x) {
int count = 0;
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x) {
count++;
mid++;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return count;
}
medium
A. Incrementing mid inside the loop does not change low or high
B. The loop condition should be low < high
C. count should be initialized to -1
D. The function should return void
Solution
Step 1: Analyze loop variables and updates
Incrementing mid does not affect low or high, so loop may become infinite or incorrect.
Step 2: Understand binary search update rules
To move search window, low or high must be updated, not mid.
Final Answer:
Incrementing mid inside the loop does not change low or high -> Option A
Quick Check:
mid++ does not move search window [OK]
Hint: Update low/high, not mid, to move binary search window [OK]
Common Mistakes:
Changing mid instead of low/high
Wrong loop condition causing infinite loop
Incorrect return type
5. Given a sorted array arr = {1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5}, write a function that uses binary search to count occurrences of x = 5. What is the count returned?
hard
A. 4
B. 6
C. 5
D. 3
Solution
Step 1: Use binary search to find first occurrence of 5
First 5 is at index 7.
Step 2: Use binary search to find last occurrence of 5
Last 5 is at index 10.
Step 3: Calculate count as last - first + 1
Count = 10 - 7 + 1 = 4.
Final Answer:
4 -> Option A
Quick Check:
Count of 5 in array = 4 [OK]
Hint: Count = last index - first index + 1 using binary search [OK]
Common Mistakes:
Counting total elements instead of occurrences
Off-by-one errors in count calculation
Using linear search instead of binary search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.