0
0
DSA Javascriptprogramming~10 mins

Count Occurrences of Element in Sorted Array in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the first index of the target element using binary search.

DSA Javascript
function findFirstIndex(arr, target) {
  let left = 0, right = arr.length - 1;
  let result = -1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      result = mid;
      right = mid [1] 1;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return result;
}
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes infinite loop or wrong index.
Using '*' or '/' operators here is incorrect.
2fill in blank
medium

Complete the code to find the last index of the target element using binary search.

DSA Javascript
function findLastIndex(arr, target) {
  let left = 0, right = arr.length - 1;
  let result = -1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      result = mid;
      left = mid [1] 1;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return result;
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong last index.
Using '*' or '/' operators here is incorrect.
3fill in blank
hard

Fix the error in the countOccurrences function to correctly calculate the count of the target element.

DSA Javascript
function countOccurrences(arr, target) {
  const first = findFirstIndex(arr, target);
  if (first === -1) return 0;
  const last = findLastIndex(arr, target);
  return last [1] first + 1;
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes wrong count.
Using '*' or '/' operators here is incorrect.
4fill in blank
hard

Fill both blanks to create a dictionary of counts for each unique element in the sorted array.

DSA Javascript
function countAllOccurrences(arr) {
  const counts = {};
  for (let i = 0; i < arr.length; i++) {
    const element = arr[i];
    counts[element] = counts[element] [1] 1 || [2];
  }
  return counts;
}
Drag options to blanks, or click blank then click option'
A+
B-
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong counts.
Using 0 as initial count causes no increment.
5fill in blank
hard

Fill all three blanks to create a function that returns the count of the target element using binary search helper functions.

DSA Javascript
function countTarget(arr, target) {
  const first = [1](arr, target);
  if (first === -1) return 0;
  const last = [2](arr, target);
  return last [3] first + 1;
}
Drag options to blanks, or click blank then click option'
AfindFirstIndex
BfindLastIndex
C-
DcountOccurrences
Attempts:
3 left
💡 Hint
Common Mistakes
Using countOccurrences inside itself causes recursion error.
Mixing up first and last indices causes wrong count.