Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the "First and Last Occurrence of Element" problem ask you to find in an array?
easy
A. The number of unique elements in the array
B. The sum of all elements in the array
C. The maximum and minimum values in the array
D. The first and last positions where a given element appears

Solution

  1. Step 1: Understand the problem statement

    The problem asks to find the first and last positions of a specific element in an array.
  2. Step 2: Identify what is being searched

    We look for the first index and last index where the element appears.
  3. Final Answer:

    The first and last positions where a given element appears -> Option D
  4. Quick Check:

    First and last occurrence = positions of element [OK]
Hint: Look for start and end indexes of the element [OK]
Common Mistakes:
  • Confusing with sum or count of elements
  • Looking for max/min values instead
  • Ignoring the order of occurrences
2. Which of the following JavaScript code snippets correctly finds the first occurrence index of element 5 in array [1, 5, 3, 5, 7]?
easy
A. array.indexOf(5)
B. array.findIndex(5)
C. array.lastIndexOf(5)
D. array.find(5)

Solution

  1. Step 1: Recall JavaScript methods for finding index

    indexOf returns the first index of the element, lastIndexOf returns the last index.
  2. Step 2: Check method usage

    array.indexOf(5) correctly returns the first occurrence index of 5.
  3. Final Answer:

    array.indexOf(5) -> Option A
  4. Quick Check:

    indexOf finds first occurrence [OK]
Hint: Use indexOf for first occurrence, lastIndexOf for last [OK]
Common Mistakes:
  • Using findIndex incorrectly with a value instead of a function
  • Using find which returns element, not index
  • Confusing lastIndexOf with first occurrence
3. What will be the output of the following code?
const arr = [2, 4, 6, 4, 8];
const first = arr.indexOf(4);
const last = arr.lastIndexOf(4);
console.log([first, last]);
medium
A. [2, 4]
B. [3, 1]
C. [1, 3]
D. [0, 4]

Solution

  1. Step 1: Find first occurrence of 4 in array

    4 appears first at index 1.
  2. Step 2: Find last occurrence of 4 in array

    4 appears last at index 3.
  3. Final Answer:

    [1, 3] -> Option C
  4. Quick Check:

    First = 1, Last = 3 [OK]
Hint: indexOf gives first, lastIndexOf gives last [OK]
Common Mistakes:
  • Mixing up first and last indexes
  • Using wrong array indexes
  • Confusing element values with indexes
4. Identify the error in the following code that tries to find the first and last occurrence of 7 in array [7, 2, 7, 5]:
const arr = [7, 2, 7, 5];
const first = arr.findIndex(7);
const last = arr.findLastIndex(7);
console.log([first, last]);
medium
A. findLastIndex is not a valid JavaScript method
B. findIndex and findLastIndex require a function, not a value
C. The array does not contain 7, so indexes are -1
D. The code is correct and will output [0, 2]

Solution

  1. Step 1: Check usage of findIndex and findLastIndex

    Both methods require a function as argument, not a direct value.
  2. Step 2: Identify the error in argument

    Passing 7 directly causes incorrect usage; should pass a function like x => x === 7.
  3. Final Answer:

    findIndex and findLastIndex require a function, not a value -> Option B
  4. Quick Check:

    Methods need function argument [OK]
Hint: Pass a function to findIndex, not a direct value [OK]
Common Mistakes:
  • Passing value instead of function to findIndex
  • Assuming findLastIndex exists in all JS versions
  • Confusing findIndex with indexOf
5. Given an array arr = [3, 5, 3, 7, 5, 3], write a function in JavaScript that returns an object with the first and last occurrence indexes of element 3. What will be the output of findOccurrences(arr, 3)?
function findOccurrences(array, element) {
  return {
    first: array.indexOf(element),
    last: array.lastIndexOf(element)
  };
}

const arr = [3, 5, 3, 7, 5, 3];
console.log(findOccurrences(arr, 3));
hard
A. {"first": 0, "last": 5}
B. {"first": 2, "last": 5}
C. {"first": 1, "last": 4}
D. {"first": 0, "last": 2}

Solution

  1. Step 1: Find first occurrence of 3 in array

    3 first appears at index 0.
  2. Step 2: Find last occurrence of 3 in array

    3 last appears at index 5.
  3. Final Answer:

    {"first": 0, "last": 5} -> Option A
  4. Quick Check:

    First = 0, Last = 5 [OK]
Hint: Use indexOf and lastIndexOf to get first and last indexes [OK]
Common Mistakes:
  • Mixing up first and last indexes
  • Returning values instead of indexes
  • Incorrectly reading array indexes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard