0
0
DSA Goprogramming~10 mins

First and Last Occurrence of Element in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - First and Last Occurrence of Element
Start
Initialize first = -1, last = -1
Traverse array from i=0 to n-1
Check if array[i
If first == -1, set first = i
Set last = i
Continue traversal
End traversal
Return first and last
We scan the array once, updating first and last positions when we find the target.
Execution Sample
DSA Go
arr := []int{2, 3, 5, 3, 7}
target := 3
first, last := -1, -1
for i := 0; i < len(arr); i++ {
  if arr[i] == target {
    if first == -1 {
      first = i
    }
    last = i
  }
}
Find first and last index of target 3 in array [2,3,5,3,7]
Execution Table
StepOperationIndex iarr[i]Condition arr[i]==targetfirstlastVisual State
1Initialize----1-1[2, 3, 5, 3, 7]
2Check arr[0]02False-1-1[2, 3, 5, 3, 7]
3Check arr[1]13True11[2, 3, 5, 3, 7]
4Check arr[2]25False11[2, 3, 5, 3, 7]
5Check arr[3]33True13[2, 3, 5, 3, 7]
6Check arr[4]47False13[2, 3, 5, 3, 7]
7End traversal---13[2, 3, 5, 3, 7]
💡 Traversal ends after checking all elements; first=1 and last=3 are returned
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
first-1-111111
last-1-111333
i-01234-
Key Moments - 3 Insights
Why do we set first only when it is -1?
Because first occurrence is the earliest index where target appears. Once set, we don't change it. See execution_table rows 3 and 5 where first is set only once.
Why do we update last every time we find the target?
Because last occurrence is the latest index where target appears. We keep updating last to current index when condition is true. See execution_table rows 3 and 5 where last changes.
What if the target is not found at all?
Both first and last remain -1, indicating target is absent. This is shown in execution_table row 2 when condition is false and no updates happen.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what are the values of first and last?
Afirst=-1, last=1
Bfirst=1, last=-1
Cfirst=1, last=1
Dfirst=-1, last=-1
💡 Hint
Check the columns 'first' and 'last' in execution_table row with Step 3
At which step does the last occurrence get updated to index 3?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look at 'last' column changes in execution_table rows; last changes to 3 at Step 5
If the target was 10 (not in array), what would be the final values of first and last?
Afirst=0, last=0
Bfirst=-1, last=-1
Cfirst=4, last=4
Dfirst=1, last=3
💡 Hint
Refer to key_moments about target absence and initial values in variable_tracker
Concept Snapshot
First and Last Occurrence of Element:
- Initialize first and last as -1
- Traverse array from start to end
- When element == target:
  - If first == -1, set first = current index
  - Always update last = current index
- After traversal, first and last hold positions or -1 if not found
Full Transcript
This concept finds the first and last positions of a target element in an array. We start with first and last set to -1, meaning not found. We scan the array from left to right. When we find the target, if first is still -1, we set it to current index. We always update last to current index when target matches. After scanning all elements, first and last tell us the earliest and latest positions of the target. If target is not found, both remain -1. This method uses one pass and simple checks.