0
0
DSA Javascriptprogramming~20 mins

Count Occurrences of Element in Sorted Array in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of counting occurrences using binary search
What is the output of the following code that counts occurrences of 3 in a sorted array?
DSA Javascript
function countOccurrences(arr, x) {
  function firstOccurrence() {
    let low = 0, high = arr.length - 1, result = -1;
    while (low <= high) {
      let mid = Math.floor((low + high) / 2);
      if (arr[mid] === x) {
        result = mid;
        high = mid - 1;
      } else if (arr[mid] < x) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    return result;
  }

  function lastOccurrence() {
    let low = 0, high = arr.length - 1, result = -1;
    while (low <= high) {
      let mid = Math.floor((low + high) / 2);
      if (arr[mid] === x) {
        result = mid;
        low = mid + 1;
      } else if (arr[mid] < x) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    return result;
  }

  let first = firstOccurrence();
  if (first === -1) return 0;
  let last = lastOccurrence();
  return last - first + 1;
}

console.log(countOccurrences([1,2,3,3,3,4,5], 3));
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Think about how binary search finds the first and last positions of the target.
Predict Output
intermediate
2:00remaining
Output when element is not present
What is the output of the code when counting occurrences of 6 in the sorted array?
DSA Javascript
function countOccurrences(arr, x) {
  function firstOccurrence() {
    let low = 0, high = arr.length - 1, result = -1;
    while (low <= high) {
      let mid = Math.floor((low + high) / 2);
      if (arr[mid] === x) {
        result = mid;
        high = mid - 1;
      } else if (arr[mid] < x) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    return result;
  }

  function lastOccurrence() {
    let low = 0, high = arr.length - 1, result = -1;
    while (low <= high) {
      let mid = Math.floor((low + high) / 2);
      if (arr[mid] === x) {
        result = mid;
        low = mid + 1;
      } else if (arr[mid] < x) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    return result;
  }

  let first = firstOccurrence();
  if (first === -1) return 0;
  let last = lastOccurrence();
  return last - first + 1;
}

console.log(countOccurrences([1,2,3,3,3,4,5], 6));
A1
Bundefined
C5
D0
Attempts:
2 left
💡 Hint
If the element is not found, the function returns 0.
🔧 Debug
advanced
2:00remaining
Identify the error in counting occurrences code
What error does the following code produce when counting occurrences of 3 in the array?
DSA Javascript
function countOccurrences(arr, x) {
  function firstOccurrence() {
    let low = 0, high = arr.length - 1, result = -1
    while (low <= high) {
      let mid = Math.floor((low + high) / 2)
      if (arr[mid] === x) {
        result = mid
        high = mid - 1
      } else if (arr[mid] < x) {
        low = mid + 1
      } else {
        high = mid - 1
      }
    }
    return result
  }

  function lastOccurrence() {
    let low = 0, high = arr.length - 1, result = -1
    while (low <= high) {
      let mid = Math.floor((low + high) / 2)
      if (arr[mid] === x) {
        result = mid
        low = mid + 1
      } else if (arr[mid] < x) {
        low = mid + 1
      } else {
        high = mid - 1
      }
    }
    return result
  }

  let first = firstOccurrence()
  if (first === -1) return 0
  let last = lastOccurrence()
  return last - first + 1
}

console.log(countOccurrences([1,2,3,3,3,4,5], 3))
AThe code runs and returns 3
BInfinite loop due to incorrect update of high and low pointers
CSyntaxError due to missing semicolons
DTypeError because result is undefined
Attempts:
2 left
💡 Hint
Check how the pointers low and high are updated inside the loops.
Predict Output
advanced
2:00remaining
Output of counting occurrences with duplicates at edges
What is the output of counting occurrences of 2 in the array with duplicates at the edges?
DSA Javascript
function countOccurrences(arr, x) {
  function firstOccurrence() {
    let low = 0, high = arr.length - 1, result = -1;
    while (low <= high) {
      let mid = Math.floor((low + high) / 2);
      if (arr[mid] === x) {
        result = mid;
        high = mid - 1;
      } else if (arr[mid] < x) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    return result;
  }

  function lastOccurrence() {
    let low = 0, high = arr.length - 1, result = -1;
    while (low <= high) {
      let mid = Math.floor((low + high) / 2);
      if (arr[mid] === x) {
        result = mid;
        low = mid + 1;
      } else if (arr[mid] < x) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    return result;
  }

  let first = firstOccurrence();
  if (first === -1) return 0;
  let last = lastOccurrence();
  return last - first + 1;
}

console.log(countOccurrences([2,2,2,3,4,5,6], 2));
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Duplicates at the start should be counted correctly.
🚀 Application
expert
2:00remaining
Count occurrences in a large sorted array efficiently
You have a sorted array of 1 million integers with many duplicates. Which approach is the most efficient to count occurrences of a given element x?
AUse binary search to find the first and last occurrence of x, then calculate the count
BUse a hash map to store counts of all elements, then retrieve count of x
CSort the array first, then count occurrences using a hash map
DUse a linear scan to count all occurrences of x in the array
Attempts:
2 left
💡 Hint
Think about the time complexity of searching in a sorted array.