a, b, ..... y, z ^ ...... ^ <- moving first ^ ...... ^ <- moving last We can combine both conditions into if and have just one loop:
int result = 0; for (int i = 0; i < A.length; ++i) if (A[i] != A[A.length - 1] || A[0] != A[A.length - 1 - i]) { result = A.length - i - 1; break; }