📖 Scenario: You are working with a list of product IDs sold in a store during a day. You want to find the first and last time a specific product was sold.
🎯 Goal: Build a Java program that finds the first and last occurrence positions of a given product ID in a list of product IDs.
📋 What You'll Learn
Create an integer array called productIds with exact values
Create an integer variable called targetProduct with a specific value
Use a for loop to find the first occurrence index of targetProduct
Use a for loop to find the last occurrence index of targetProduct
Print the first and last occurrence indices exactly as specified
💡 Why This Matters
🌍 Real World
Finding the first and last occurrence of an item in a list is useful in sales data analysis, inventory tracking, and event logs.
💼 Career
This skill helps in data processing tasks, debugging, and writing efficient search algorithms in software development.
Progress0 / 4 steps
1
Create the product IDs array
Create an integer array called productIds with these exact values: 3, 5, 7, 5, 9, 5, 2
DSA Java
Hint
Use curly braces {} to list the values inside the array.
2
Set the target product ID
Create an integer variable called targetProduct and set it to 5
DSA Java
Hint
Use int targetProduct = 5; to create the variable.
3
Find first and last occurrence of the target product
Use a for loop with variable i from 0 to productIds.length to find the first occurrence index of targetProduct and store it in firstIndex. Then use another for loop with variable j from productIds.length - 1 down to 0 to find the last occurrence index of targetProduct and store it in lastIndex. Initialize firstIndex and lastIndex to -1 before the loops.
DSA Java
Hint
Use one loop from start to find first occurrence, and one loop from end to find last occurrence.
4
Print the first and last occurrence indices
Print the first occurrence index using System.out.println("First occurrence: " + firstIndex); and the last occurrence index using System.out.println("Last occurrence: " + lastIndex);
DSA Java
Hint
Use System.out.println to print the results exactly as shown.
Practice
(1/5)
1. What does the term first occurrence of an element in an array mean?
easy
A. The index of the first time the element appears in the array
B. The total count of the element in the array
C. The last index where the element appears
D. The sum of all elements before the element
Solution
Step 1: Understand the meaning of first occurrence
The first occurrence is the earliest position where the element appears in the array, starting from index 0.
Step 2: Compare with other options
Counting or last index are different concepts; sum is unrelated.
Final Answer:
The index of the first time the element appears in the array -> Option A
Quick Check:
First occurrence = first index found [OK]
Hint: First occurrence means earliest index found [OK]
Common Mistakes:
Confusing first occurrence with count
Mixing first occurrence with last occurrence
Thinking it means sum or total
2. Which of the following Java code snippets correctly finds the first occurrence of target in array arr?