Count Occurrences of Element in Sorted Array
📖 Scenario: You have a sorted list of product IDs from a store's sales records. You want to find out how many times a specific product was sold.
🎯 Goal: Build a program that counts how many times a given product ID appears in the sorted array.
📋 What You'll Learn
Create an array called
productIDs with the exact values: [101, 101, 102, 103, 103, 103, 104, 105]Create a variable called
targetID and set it to 103Use a
for loop with variable id to iterate over productIDsInside the loop, count how many times
targetID appearsPrint the count with
console.log💡 Why This Matters
🌍 Real World
Counting how many times a product was sold helps stores understand popular items and manage inventory.
💼 Career
This skill is useful for data analysis, software development, and any job that involves processing lists of data efficiently.
Progress0 / 4 steps