public static void Main()
new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
new[] {1, 2, 3, 3, 3, 3, 3, 5},
foreach (int[] input in inputs)
foreach (var val in new int[] { 1, -1, 5, 3, 2, 10, 100 })
Console.WriteLine(String.Join(",", input));
Console.WriteLine("first index of {0} is {1}", val, FirstIndexOf(input, val));
public static int FirstIndexOf(int[] arr, int val)
int endIndex = arr.Length - 1;
while (startIndex < endIndex)
int midPointIndex = (int)Math.Floor((decimal) ((endIndex + startIndex + 1) / 2));
if (arr[midPointIndex] > val)
endIndex = midPointIndex - 1;
else if (arr[midPointIndex] < val)
startIndex = midPointIndex;
else if (arr[midPointIndex - 1] >= val)
endIndex = midPointIndex;
if (arr[startIndex] == val)
if (startIndex == 0 || arr[startIndex - 1] == val)