Complete the code to start the linear search loop correctly.
for (int [1] = 0; [1] < arr.Length; [1]++) { if (arr[[1]] == target) { return [1]; } }
The variable i is commonly used as the loop counter in C# for loops. It must be declared in the for loop to iterate over the array indices.
Complete the code to return -1 when the target is not found.
public int LinearSearch(int[] arr, int target) {
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == target) {
return i;
}
}
return [1];
}Returning -1 is a common way to indicate that the target was not found in the array.
Fix the error in the loop condition to avoid runtime errors.
for (int i = 0; i [1] arr.Length; i++) { if (arr[i] == target) { return i; } }
The loop should run while i is less than arr.Length because array indices go from 0 to Length-1.
Fill both blanks to complete the linear search method signature and return type.
public [1] LinearSearch([2][] arr, int target) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == target) { return i; } } return -1; }
The method returns an int index and takes an array of int as input.
Fill all three blanks to complete the code that populates a dictionary mapping elements to their indices if they match the target.
var result = new Dictionary<int, int>(); for (int [3] = 0; [3] < arr.Length; [3]++) { if (arr[[3]] == target) { result[[1]] = [2]; } }
This creates a dictionary where keys are the matching elements and values are their indices. The loop variable is i.
