In Java, Arrays.mismatch(a, b) walks both arrays from index 0 and returns the index of the first position where they differ. If the arrays are equal, it returns -1.
If one array is shorter and every element it has matches the start of the other, the first "difference" is the missing element, so the result is the length of the shorter array. That also makes mismatch a quick way to measure the length of a common prefix.
A static method of java.util.Arrays (Java 9+): call it on the class, as in Arrays.mismatch(...); there is no Arrays object to create.
Java 9+ only.
Added in Java 9: the examples need JDK 9 or newer, and on Java 8 the call fails with "cannot find symbol". On Java 8, loop from index 0 until a[i] != b[i] or the shorter length is reached; Arrays.equals only tells you whether a difference exists.
Searches two ranges; the result is relative to the range starts.
Arrays.mismatch(x, y, comparator)
Object elements differ when the comparator does not return 0.
Arrays.mismatch(a, b) == -1
Same answer as Arrays.equals(a, b) for non-null arrays.
Overloads exist for every primitive type, boolean[], Object[] (elements compared with Objects.equals), and T[] with a Comparator, each with a range form.
Library declarations
Java
// java.util.Arrays (since Java 9)
public static int mismatch(int[] a, int[] b)
public static int mismatch(int[] a, int aFromIndex, int aToIndex,
int[] b, int bFromIndex, int bToIndex)
public static int mismatch(Object[] a, Object[] b)
public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp)
// also boolean[], byte[], short[], char[], long[], float[], double[]
Parameters
Parameter
Description
a, b
The arrays to compare. Neither may be null.
range indexes (optional)
From (inclusive) and to (exclusive) indexes for each array.
cmp (optional)
Comparator that decides when two object elements match.
Return Value
Return Value
Description
-1
The arrays (or ranges) have the same length and equal elements.
int ≥ 0
Index of the first difference; equals the shorter length when one is a prefix of the other.
Compare an expected sequence with an actual one and report the first wrong value.
Java
import java.util.Arrays;
public class ArraysMismatchDemo {
public static void main(String[] args) {
int[] expected = {4, 8, 15, 16, 23, 42};
int[] actual = {4, 8, 15, 61, 23, 42};
int i = Arrays.mismatch(expected, actual);
System.out.println("first difference at index " + i);
System.out.println("expected " + expected[i] + " but got " + actual[i]);
System.out.println("copy: " + Arrays.mismatch(expected, expected.clone()));
}
}
Output
first difference at index 3
expected 16 but got 61
copy: -1
How It Works
Matching start. Indexes 0 to 2 hold 4, 8, and 15 in both arrays.
First difference. At index 3, 16 does not equal 61, so mismatch returns 3 and stops looking.
Use the index. Reading expected[i] and actual[i] gives a useful message, the kind a test framework prints.
No difference. A clone has identical contents, so the result is -1; always check for -1 before using the result as an index.
Example 2 — Common Prefix of Words as char Arrays
Measure how many leading letters two words share, including a word that is a prefix of another.
Java
import java.util.Arrays;
public class ArraysMismatchPrefix {
public static void main(String[] args) {
char[] a = "interview".toCharArray();
char[] b = "internet".toCharArray();
char[] c = "inter".toCharArray();
int ab = Arrays.mismatch(a, b);
System.out.println("a vs b: " + ab + " -> common prefix \"" + new String(a, 0, ab) + "\"");
System.out.println("a vs c: " + Arrays.mismatch(a, c));
System.out.println("c vs c: " + Arrays.mismatch(c, c));
}
}
Output
a vs b: 5 -> common prefix "inter"
a vs c: 5
c vs c: -1
How It Works
Letters as an array.toCharArray() gives each word as a char[], so the char[] overload is used.
Prefix length. "interview" and "internet" share i, n, t, e, r and differ at index 5 ('v' vs 'n'), so 5 is also the length of the common prefix.
Shorter array runs out. "inter" matches the first five letters of "interview" and then ends, so the result is its length, 5, not -1.
Same array. Comparing c with itself finds no difference and returns -1.
Example 3 — null Elements and Range Comparison
Compare String[] arrays that contain null, then compare the tail of one log with another.
Java
import java.util.Arrays;
public class ArraysMismatchRange {
public static void main(String[] args) {
String[] left = {"x", null, "y", "z"};
String[] right = {"x", null, "y", "Z"};
System.out.println("full arrays: " + Arrays.mismatch(left, right));
int[] log1 = {0, 0, 7, 8, 9};
int[] log2 = {7, 8, 1};
System.out.println("log1[2..5) vs log2[0..3): " + Arrays.mismatch(log1, 2, 5, log2, 0, 3));
}
}
Output
full arrays: 3
log1[2..5) vs log2[0..3): 2
How It Works
null matches null. Object elements are compared with Objects.equals, so the two null values at index 1 count as equal.
Case-sensitive strings. "z" and "Z" are different strings, so the first difference is at index 3.
Two ranges. The range form compares {7, 8, 9} from log1 with {7, 8, 1} from log2.
Relative index. The difference is at position 2 of each range (array index 4 in log1), and 2 is what is returned.
Frequently Asked Questions
-1. Any other result is the index of the first difference.
If the shorter array matches the start of the longer one, mismatch returns the length of the shorter array.
No. A null array throws NullPointerException. null elements inside object arrays are fine.
Java 9. On Java 8, write a loop that stops at the first index where the elements differ.
Relative: it counts from the start of each range, so add aFromIndex to get the index in the first array.