Developing Algorithms Using Arrays

AP Computer Science A· difficulty 2/5

public static int indexOf(int[] a, int target) {
    for (int i = 0; i < a.length; i++) {
        if (/* missing */) return i;
    }
    return -1;
}

Which condition replaces /* missing */?

  • A

    a[target] == i

  • B

    a == target

  • C

    i == target

  • D

    a[i] == target

    check_circle

Explanation

Compare the current array element to the target value, returning the index when they match.

Want 10 more like this — adaptive to your weak spots?

Related questions