0
0
DSA Javascriptprogramming~10 mins

First and Last Occurrence of Element in DSA Javascript - 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 end
Check if array[i
Yes
If first == -1, set first = i
Set last = i
i = i+1
Repeat until i == array.length
Return first and last
End
We scan the array once, updating first and last positions when we find the target element.
Execution Sample
DSA Javascript
function findFirstLast(arr, target) {
  let first = -1, last = -1;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) {
      if (first === -1) first = i;
      last = i;
    }
  }
  return [first, last];
}
This code finds the first and last index of the target element in the array.
Execution Table
StepOperationIndex iarr[i]Condition arr[i] == targetfirstlastVisual State
1Initialize first and last----1-1first=-1, last=-1
2Check arr[0]05No-1-1No change
3Check arr[1]13No-1-1No change
4Check arr[2]27Yes22first=2, last=2
5Check arr[3]37Yes23last updated to 3
6Check arr[4]42No23No change
7Check arr[5]57Yes25last updated to 5
8End of array---25Return [2,5]
💡 Reached end of array, all elements checked.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
first-1-1-122222
last-1-1-123355
i-012345-
Key Moments - 3 Insights
Why do we check if first == -1 before setting it?
Because first should store the earliest index where target appears. We only set it once at the first occurrence (see Step 4 in execution_table).
Why do we update last every time we find the target?
Because last should always point to the most recent occurrence of the target (see Steps 4, 5, 7). This way, after the loop, last is the last occurrence.
What if the target is not in the array at all?
first and last remain -1, indicating target not found (see initial values in Step 1 and no updates in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'first' after Step 5?
A3
B-1
C2
D5
💡 Hint
Check the 'first' column in execution_table row for Step 5.
At which step does the condition arr[i] == target become true for the first time?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Condition arr[i] == target' column in execution_table.
If the target was 10 instead of 7, what would be the final values of first and last?
A[2, 5]
B[-1, -1]
C[0, 0]
D[5, 5]
💡 Hint
Refer to variable_tracker and key_moments about target not found.
Concept Snapshot
Find first and last occurrence of an element in an array:
- Initialize first and last as -1
- Loop through array indices
- If element matches target:
  - If first == -1, set first = current index
  - Always update last = current index
- After loop, return [first, last]
- If target not found, both remain -1
Full Transcript
This concept finds the first and last positions of a target element in an array by scanning it once. We start with first and last set to -1, meaning not found. As we check each element, if it matches the target, we set first if it's the first time, and update last every time. After checking all elements, we return the positions. If the target is not found, both remain -1. This is efficient and simple to implement.