Utilities: Arrays.mismatch finds the first index where two arrays differ

Arrays.mismatch compares two arrays element by element and returns the index of the first difference, or -1 if one is a prefix of the other or they are identical, which is faster than writing a manual loop.

Code
int[] left = {1, 2, 3, 9, 5};
int[] right = {1, 2, 3, 4, 5};
System.out.println("First mismatch: " + Arrays.mismatch(left, right));
int[] same = {1, 2, 3};
System.out.println("Identical arrays: " + Arrays.mismatch(same, same));
Output
First mismatch: 3
Identical arrays: -1
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms