Q:

Finding Consecutive Matching Elements in Arrays

I am having a slight issue with this problem. All the answers check out for all of the calls except for one, which I don't get.
contains({1, 2, 1, 2, 3}, {1, 2, 3}) returns false when it should be true.
Prompt:
Write a static method named contains that accepts two arrays of integers a1 and a2 as parameters and that returns a boolean value indicating whether or not a2's sequence of elements appears in a1 (true for yes, false for no). The sequence of elements in a2 may appear anywhere in a1 but must appear consecutively and in the same order. For example, if variables called list1 and list2 store the following values:
int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8};
int[] list2 = {1, 2, 1};

Then the call of contains(list1, list2) should return true because list2's sequence of values {1, 2, 1} is contained in list1 starting at index 5. If list2 had stored the values {2, 1, 2}, the call of contains(list1, list2) would return false because list1 does not contain that sequence of values. Any two lists with identical elements are considered to contain each other, so a call such as contains(list1, list1) should return true.
You may assume that both arrays passed to your method will have lengths of at least 1. You may not use any Strings to help you solve this problem, nor methods that produce Strings such as Arrays.toString.
Code:
public static boolean contains(int[] first, int[] second) {
    int secondCount = 0; // Indicates where to start on second array

    if (first.length < second.length) { // If second array is longer than first
        return false;
    }

    for (int i=0; i<first.length; i++) { // goes through each element in first array
        if (first[i] == second[secondCount]) { // If elements match 
            secondCount++; // increment by one and move onto next elem on second
            if (secondCount == second.length) { // If complete match
                return true;
            }
        } else { // resets count
            secondCount = 0;
        }
    }
    return false;
}

A:

Your method returns false due to the way you have constructed your loop. Below, I outline your method's runtime.
i = 0; secondCount = 0;
{1, 2, 1, 2, 3} {1, 2, 3}
 ^               ^
 1 == 1 ? true

i = 1; secondCount = 1;
{1, 2, 1, 2, 3} {1, 2, 3}
    ^               ^
 2 == 2 ? true

i = 2; secondCount = 2;
{1, 2, 1, 2, 3} {1, 2, 3}
       ^               ^
 1 == 3 ? false

i = 3; secondCount = 0;
{1, 2, 1, 2, 3} {1, 2, 3}
          ^      ^
 2 == 1 ? false

i = 3; secondCount = 0;
{1, 2, 1, 2, 3} {1, 2, 3}
             ^   ^
 3 == 1 ? false

return false;

The problem is that if the boolean equation first[i] == second[secondCount] is false, then secondCount is reset while i is still advanced. Thus, contains({1, 2, 1, 2, 3}, {1, 2, 3}); will always return false.
Consider adding a continue to your code whenever first[i] == second[secondCount] fails to prevent i advancing.

