0
0
DSA Javascriptprogramming~20 mins

First and Last Occurrence of Element in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
First and Last Occurrence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find first and last occurrence indexes
What is the output of the following JavaScript code that finds the first and last occurrence of the number 3 in the array?
DSA Javascript
const arr = [1, 3, 5, 3, 7, 3, 9];
const target = 3;

function firstLastOccurrence(array, x) {
  let first = -1;
  let last = -1;
  for (let i = 0; i < array.length; i++) {
    if (array[i] === x) {
      if (first === -1) first = i;
      last = i;
    }
  }
  return { first, last };
}

console.log(firstLastOccurrence(arr, target));
A{"first":1,"last":3}
B{"first":2,"last":5}
C{"first":0,"last":6}
D{"first":1,"last":5}
Attempts:
2 left
💡 Hint
Remember array indexes start at 0 and check all positions where the target appears.
🧠 Conceptual
intermediate
2:00remaining
Understanding output when element is absent
What will be the output of the function when the target element is not present in the array?
DSA Javascript
const arr = [2, 4, 6, 8];
const target = 5;

function firstLastOccurrence(array, x) {
  let first = -1;
  let last = -1;
  for (let i = 0; i < array.length; i++) {
    if (array[i] === x) {
      if (first === -1) first = i;
      last = i;
    }
  }
  return { first, last };
}

console.log(firstLastOccurrence(arr, target));
A{"first":-1,"last":-1}
B{"first":0,"last":0}
C{"first":null,"last":null}
Dundefined
Attempts:
2 left
💡 Hint
Check what the initial values of first and last are and if they change when the element is not found.
Predict Output
advanced
2:00remaining
Output of recursive first occurrence search
What is the output of this recursive function that finds the first occurrence of a target in an array?
DSA Javascript
function firstOccurrenceRecursive(arr, target, index = 0) {
  if (index === arr.length) return -1;
  if (arr[index] === target) return index;
  return firstOccurrenceRecursive(arr, target, index + 1);
}

const array = [4, 2, 7, 2, 9];
console.log(firstOccurrenceRecursive(array, 2));
A3
B1
C-1
D2
Attempts:
2 left
💡 Hint
The function checks elements from the start and returns the first index where the target is found.
Predict Output
advanced
2:00remaining
Output of last occurrence using reverse loop
What is the output of this code that finds the last occurrence of the target 5 in the array by looping backwards?
DSA Javascript
const arr = [5, 1, 5, 3, 5, 7];
const target = 5;

function lastOccurrence(array, x) {
  for (let i = array.length - 1; i >= 0; i--) {
    if (array[i] === x) return i;
  }
  return -1;
}

console.log(lastOccurrence(arr, target));
A4
B-1
C2
D0
Attempts:
2 left
💡 Hint
The loop starts from the end and returns the first matching index it finds.
🚀 Application
expert
3:00remaining
Count elements between first and last occurrence
Given an array and a target element, what is the output of the code that counts how many elements are between the first and last occurrence of the target (exclusive)?
DSA Javascript
const arr = [1, 3, 5, 3, 7, 3, 9];
const target = 3;

function countBetweenFirstLast(array, x) {
  let first = -1;
  let last = -1;
  for (let i = 0; i < array.length; i++) {
    if (array[i] === x) {
      if (first === -1) first = i;
      last = i;
    }
  }
  if (first === -1 || last === -1 || first === last) return 0;
  return last - first - 1;
}

console.log(countBetweenFirstLast(arr, target));
A5
B2
C3
D4
Attempts:
2 left
💡 Hint
Count the number of elements strictly between the first and last occurrence indexes.