Created
August 30, 2011 15:37
-
-
Save OpenGrid/1181192 to your computer and use it in GitHub Desktop.
Count all occurences of a number in ascending array of integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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