First and Last Occurrence of Element
📖 Scenario: You are working with a list of product IDs scanned at a store checkout. Some products appear multiple times. You want to find the first and last time a specific product was scanned.
🎯 Goal: Build a Go program that finds the first and last position of a given product ID in a list.
📋 What You'll Learn
Create a slice of integers called
products with the exact values: 101, 203, 101, 405, 203, 101Create an integer variable called
target and set it to 101Write a loop to find the first and last index of
target in productsPrint the first and last occurrence indices in the format:
First: X, Last: Y💡 Why This Matters
🌍 Real World
Finding first and last occurrences helps in searching logs, tracking events, or analyzing repeated data in real applications.
💼 Career
This skill is useful for software developers working with arrays, slices, or lists to analyze data and implement search features.
Progress0 / 4 steps