0
0
DSA Typescriptprogramming~10 mins

First and Last Occurrence of Element in DSA Typescript - 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
If first == -1, set first = i
Set last = i
Continue traversal
End traversal
Return first and last indices
Done
We scan the array once, updating first and last positions when we find the target element.
Execution Sample
DSA Typescript
function findFirstLast(arr: number[], target: number): [number, number] {
  let first = -1;
  let 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 positions of the target number in the array.
Execution Table
StepOperationIndex iarr[i]Condition arr[i] == targetfirstlastVisual State
1Initialize first and last----1-1first = -1, last = -1
2Check element05No-1-1No change
3Check element13Yes11first = 1, last = 1
4Check element27No11No change
5Check element33Yes13last updated to 3
6Check element49No13No change
7Check element53Yes15last updated to 5
8End traversal---15Return [1, 5]
💡 Reached end of array, traversal stops
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
first-1-1111111
last-1-1113355
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 3 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 3, 5, 7 in execution_table).
What if the target is not in the array at all?
Then 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?
A1
B3
C-1
D5
💡 Hint
Check the 'first' column in row for Step 5 in execution_table.
At which step does the condition arr[i] == target become false for the first time?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Condition arr[i] == target' column in execution_table rows.
If the target was 7 instead of 3, what would be the final value of 'last'?
A-1
B2
C5
D3
💡 Hint
Check where 7 appears in the array and see last updates in execution_table.
Concept Snapshot
Find first and last occurrence of target in array:
- Initialize first and last to -1
- Traverse array once
- If arr[i] == target:
  - If first == -1, set first = i
  - Update last = i
- Return [first, last]
- If target not found, returns [-1, -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 move through the array, when we find the target for the first time, we set first to that index. Every time we find the target, we update last to that index. After scanning all elements, we return the two indices. If the target is not found, both remain -1. The execution table shows each step with index, element checked, condition result, and updates to first and last. This helps visualize how the pointers move and how the final result is formed.