Discover how to find exactly where something starts and ends in a list without endless searching!
Why First and Last Occurrence of Element in DSA Java?
Imagine you have a long list of names written on paper. You want to find where a particular name appears first and last in the list. Doing this by scanning the list every time is tiring and slow.
Looking through the list manually each time wastes time and can cause mistakes, especially if the list is very long or if the name appears many times. You might miss the first or last place it appears.
Using a simple method to find the first and last place an element appears in a list helps you quickly get the exact positions without checking every item again and again.
int first = -1; for (int i = 0; i < arr.length; i++) { if (arr[i].equals(target)) { first = i; break; } } int last = -1; for (int i = arr.length - 1; i >= 0; i--) { if (arr[i].equals(target)) { last = i; break; } }
int first = findFirstOccurrence(arr, target); int last = findLastOccurrence(arr, target);
This lets you quickly find where an element starts and ends in a list, making searching and data handling much faster and easier.
Think about searching for a word in a book to see where it first appears and where it last appears to understand its importance or context.
Manual searching is slow and error-prone.
Finding first and last occurrence helps locate elements quickly.
This method saves time and reduces mistakes in searching.
Practice
first occurrence of an element in an array mean?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 AQuick Check:
First occurrence = first index found [OK]
- Confusing first occurrence with count
- Mixing first occurrence with last occurrence
- Thinking it means sum or total
target in array arr?Solution
Step 1: Check loop direction and condition
for(int i=0; iStep 2: Analyze other options
for(int i=arr.length-1; i>=0; i--) { if(arr[i] == target) return i; } return -1; loops backward, so finds last occurrence. int index = arr.indexOf(target); return index; uses invalid method for arrays. while(arr.contains(target)) { return arr.indexOf(target); } has incorrect syntax and logic.Final Answer:
for(int i=0; i -> Option AQuick Check:
Loop start to end returns first match [OK]
- Looping backward to find first occurrence
- Using methods not available for arrays
- Incorrect loop conditions
int[] arr = {5, 3, 7, 3, 9};
int target = 3;
int first = -1, last = -1;
for(int i=0; iSolution
Step 1: Trace the loop to find first and last indices
At i=1, arr[1]=3 matches target, first=-1 so first=1, last=1. At i=3, arr[3]=3 matches, last=3 updated.Step 2: Confirm final values
First occurrence is index 1, last occurrence is index 3.Final Answer:
First: 1, Last: 3 -> Option BQuick Check:
First=1, Last=3 from loop [OK]
- Swapping first and last values
- Not updating last occurrence correctly
- Initializing first incorrectly
target in arr:int last = -1; for(int i=0; i
Solution
Step 1: Analyze loop and break usage
The break inside if stops loop at first match, so last is actually first occurrence, not last.Step 2: Understand correct approach
To find last occurrence, loop should continue through entire array without break, updating last each time.Final Answer:
Break statement stops loop too early, finds first not last occurrence -> Option DQuick Check:
Break stops loop early, wrong for last occurrence [OK]
- Using break inside loop for last occurrence
- Looping forward but breaking early
- Confusing first and last occurrence logic
arr with possible duplicates, which approach efficiently finds the first and last occurrence of target in O(log n) time?Solution
Step 1: Understand array is sorted
Sorted array allows binary search to find elements quickly in O(log n) time.Step 2: Apply binary search twice
One binary search finds first occurrence by moving left on matches; another finds last occurrence by moving right.Step 3: Compare with other options
Linear traversal is O(n), HashMap adds overhead, sorting again is unnecessary.Final Answer:
Use binary search twice: once for first occurrence, once for last occurrence -> Option CQuick Check:
Binary search twice = O(log n) efficient [OK]
- Using linear search on sorted array
- Trying to sort array again unnecessarily
- Using extra data structures without need
Ready to prove it?
Pass the test and this pattern moves unknown
