Originally Posted by
radulescuiulia
...The problem is that ...the only solution I've found...
A bigger problem is that your function is bugous.
I mean, if your binary search just happens to land on the first of two occurrences and there are exactly two occurrences, well, OK, but. a few runs with different points of occurrence shows some incorrect counts:
for v = [8, 9, 10, 11, 12, 13, 14]:
countOccurrences(7) = 0 <---> OK!
for v = [7, 8, 9, 10, 11, 12, 13]:
countOccurrences(7) = 0 <---> Huh? Should be 1
for v = [1, 7, 9, 10, 11, 12, 13]:
countOccurrences(7) = 1 <---> OK!
for v = [1, 2, 7, 10, 11, 12, 13]:
countOccurrences(7) = 1 <---> OK!
for v = [1, 2, 3, 7, 11, 12, 13]:
countOccurrences(7) = 1 <---> OK!
for v = [1, 2, 3, 4, 7, 12, 13]:
countOccurrences(7) = 1 <---> OK!
for v = [1, 2, 3, 4, 6, 7, 13]:
countOccurrences(7) = 1 <---> OK!
for v = [1, 2, 3, 4, 5, 6, 7]:
countOccurrences(7) = 1 <---> OK!
for v = [7, 7, 9, 10, 11, 12, 13]:
countOccurrences(7) = 1 <---> Huh? Should be 2
for v = [1, 7, 7, 10, 11, 12, 13]:
countOccurrences(7) = 1 <---> Huh? Should be 2
for v = [1, 2, 7, 7, 11, 12, 13]:
countOccurrences(7) = 2 <---> OK!
for v = [1, 2, 3, 7, 7, 12, 13]:
countOccurrences(7) = 1 <---> Huh? Should be 2
for v = [1, 2, 3, 4, 7, 7, 13]:
countOccurrences(7) = 2 <---> OK!
for v = [1, 2, 3, 4, 6, 7, 7]:
countOccurrences(7) = 1 <---> Huh? Should be 2
for v = [7, 7, 7, 10, 11, 12, 13]:
countOccurrences(7) = 1 <---> Huh? Should be 3
for v = [1, 7, 7, 7, 11, 12, 13]:
countOccurrences(7) = 2 <---> Huh? Should be 3
.
.
.
// Etc.
.
.
.
// With a seven-element array:
// Your method can not get the correct count value for occurrences greater than two.
.
.
.
Cheers!
Z