Index of 6 or -1.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
int[] arr = { 3, 6, 9, 12, 15, 18, 21, 24 }; int target = 6;
int idx = -1;
for (int j = 0; j < arr.Length; j++) if (arr[j] == target) { idx = j; break; }
Console.WriteLine(idx);
}
}
Try solving on your own first, then reveal the official answer.
Linear search O(n).