Count Occurrences of Element in Sorted Array
📖 Scenario: You have a sorted list of product IDs representing items sold in a store. You want to find out how many times a specific product ID appears in this list.
🎯 Goal: Build a program that counts how many times a given product ID appears in a sorted array of product IDs.
📋 What You'll Learn
Create an array called
product_ids with the exact values: {101, 102, 102, 102, 103, 104, 104, 105}Create an integer variable called
target_id and set it to 102Write a function called
countOccurrences that takes the array product_ids, its size, and target_id as parameters and returns the count of target_id in the arrayPrint the count returned by
countOccurrences💡 Why This Matters
🌍 Real World
Counting how many times a product appears in sales data helps stores understand product popularity and manage inventory.
💼 Career
This skill is useful for software developers working with data analysis, inventory management systems, and any application needing frequency counts in sorted data.
Progress0 / 4 steps