I want to get all the position of (8) in a array like this: (3,5,6,7,8,8,9,33,34,45). But my code returns only one position and forget the second one:
This is my binary search code:
private static int BinarySearch(int[] array, int item)
{
int left = 0;
int right = array.Length - 1;
while (left <= right)
{
var middle = (left + right) / 2;
if (array[middle] == item)
return middle;
if (item < array[middle])
right = middle - 1;
else
left = middle + 1;
}
return -1;
}