This is an open source platform.
Create coding videos, upload them on YouTube, and help the community grow.
Teaching is the fastest way to learn β and the best way to never forget
what you know π
β‘ Start Recording
Screen only
Screen With Camera
β‘Start Recording
β
Selected Template
${temp1}
Screen only
Screen With Camera
Start Recording
β
β‘ Show Preview
Thumbnail Templates
Generating Thumbnail...
Master In-Demand Tech. Get Paid
Count Occurrences in Sorted Array
Given a sorted array and a target, return the number of times the target appears.
Difficulty:
JavaScript Solution
function countOccurrences(nums, target) {
const first = firstOccurrence(nums, target);
if (first === -1) return 0;
const last = lastOccurrence(nums, target);
return last - first + 1;
}
Explanation: Count = last occurrence - first occurrence + 1.