First and Last Occurrence of Element
📖 Scenario: You are working with a list of product IDs sold in a store during a day. Some products appear multiple times because they were sold multiple times.You want to find the first and last time a specific product was sold to understand its sales pattern.
🎯 Goal: Build a program that finds the first and last position of a given product ID in the sales list.
📋 What You'll Learn
Create an array called
sales with the exact product IDs: [101, 203, 101, 405, 203, 101, 507]Create a variable called
targetProduct and set it to 101Write a loop to find the first and last index of
targetProduct in salesPrint the first and last occurrence indexes in the format:
First occurrence: X, Last occurrence: Y💡 Why This Matters
🌍 Real World
Stores and businesses often analyze sales data to understand when products are sold during the day. Finding first and last occurrences helps in tracking product demand and stock management.
💼 Career
This skill is useful for data analysts, software developers, and anyone working with data searching and processing in real-world applications.
Progress0 / 4 steps