Index of 6 or -1.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; 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).