Discover how to instantly pinpoint where something starts and ends in a list without endless searching!
Why First and Last Occurrence of Element in DSA Typescript?
Imagine you have a long list of names written on paper, and you want to find where a particular name first appears and where it appears last.
Doing this by scanning the list every time someone asks is tiring and slow.
Manually searching through the list means checking each name one by one.
This takes a lot of time if the list is long, and you might lose track or make mistakes.
Using the method to find the first and last occurrence of an element helps you quickly locate where the element starts and ends in the list.
This saves time and reduces errors by automating the search.
let index = -1; for(let i = 0; i < arr.length; i++) { if(arr[i] === target) { index = i; break; } }
function findFirstAndLast(arr: number[], target: number): [number, number] {
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 lets you quickly find the exact range where an element appears, enabling faster data analysis and decision-making.
In a list of daily temperatures, finding the first and last day a certain temperature occurred helps understand weather patterns.
Manual search is slow and error-prone for large lists.
Finding first and last occurrence automates and speeds up locating elements.
This method helps in many real-world data tracking tasks.
Practice
Solution
Step 1: Understand the problem statement
The problem asks to find the first and last index positions of a specific element in an array.Step 2: Identify what is required
We need to locate where the element appears first and last, not sums or counts.Final Answer:
The first and last positions where a given element appears -> Option CQuick Check:
First and last occurrence = positions of element [OK]
- Confusing occurrence with sum or max
- Thinking about unique elements instead
- Ignoring the order of elements
Solution
Step 1: Analyze the function parameters
The function should take an array of numbers and a target number to find.Step 2: Analyze the return type
We want to return two numbers (first and last index), so a tuple [number, number] is appropriate.Final Answer:
function findOccurrences(arr: number[], target: number): [number, number] -> Option DQuick Check:
Input array and target, output tuple of two indexes [OK]
- Using wrong input types like single number instead of array
- Returning only one number instead of two
- Returning array of wrong type
function findOccurrences(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];
}
console.log(findOccurrences([5, 3, 7, 3, 9], 3));Solution
Step 1: Trace the loop to find first occurrence
At index 1, element is 3 which matches target, so first = 1.Step 2: Update last occurrence while looping
At index 3, element is 3 again, so last = 3.Final Answer:
[1, 3] -> Option AQuick Check:
First = 1, Last = 3 [OK]
- Returning first occurrence twice
- Returning indexes of wrong elements
- Not updating last occurrence properly
function findOccurrences(arr: number[], target: number): [number, number] {
let first = 0;
let last = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
if (first === 0) first = i;
last = i;
}
}
return [first, last];
}Solution
Step 1: Check initialization of first
first is set to 0, so if target is at index 0, condition first === 0 is true initially, causing incorrect logic.Step 2: Understand impact on first occurrence
If target is at index 0, first will never update correctly because first === 0 is true initially, so first stays 0 even if target not found.Final Answer:
first initialized to 0 causes wrong first occurrence when target is at index 0 -> Option BQuick Check:
Initialize first to -1 to fix [OK]
- Initializing first to 0 instead of -1
- Not handling target at index 0 correctly
- Assuming first occurrence is always > 0
Solution
Step 1: Understand the array is sorted
Since the array is sorted, binary search can be used to find elements efficiently.Step 2: Use binary search twice
One binary search finds the first occurrence, another finds the last occurrence, both in O(log n) time.Step 3: Compare with other options
Traversing is O(n), sorting again is unnecessary, hash map uses extra space and is less efficient.Final Answer:
Use two binary searches: one for first occurrence, one for last occurrence -> Option AQuick Check:
Binary search twice = efficient for sorted arrays [OK]
- Using linear search on sorted array
- Sorting array again unnecessarily
- Using extra space with hash map
Ready to prove it?
Pass the test and this pattern moves unknown
