Skip to content

Instantly share code, notes, and snippets.

@OpenGrid
Created August 30, 2011 15:37
Show Gist options
  • Save OpenGrid/1181192 to your computer and use it in GitHub Desktop.
Save OpenGrid/1181192 to your computer and use it in GitHub Desktop.
Count all occurences of a number in ascending array of integers
var binarysearch = function(number, ascendingIntegers) {
var current, end = ascendingIntegers.length, middle, count = 0;
for(current = 0; current < end; current++) {
middle = (current + end) / 2 | 0;
if(ascendingIntegers[middle] < number)
current = middle;
else if(ascendingIntegers[middle] > number)
end = middle;
if(ascendingIntegers[current] === number)
count++;
}
return count;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment