Do you care about duplicate counts? For example, would you need to distinguish between { 1, 1, 2 } and { 1, 2, 2 }? If not, just use a HashSet:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1));
    HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(arr2));
    return set1.equals(set2);
}

If you do care about duplicates, then either you could use a Multiset from Guava.

If you want to stick with the sorting version, why not use the built-in sorting algorithms instead of writing your own?

EDIT: You don't even need to create a copy, if you're happy modifying the existing arrays. For example:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}

You can also have an optimization for the case where the arrays aren't the same length:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    // TODO: Null validation...
    if (arr1.length != arr2.length) {
        return false;
    }
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}
Answer from Jon Skeet on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-check-if-two-arrays-are-equal-or-not
Java Program to Check if two Arrays are Equal or not - GeeksforGeeks
August 19, 2026 - The Arrays.equals() method is the simplest way to compare two one-dimensional arrays. It checks whether both arrays have the same length and equal elements at corresponding positions.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-check-if-two-arrays-contain-the-same-elements-in-java
How to check if two arrays contain the same elements in Java
In lines 10 to 12 we sorted the three object arrays to ensure the order of elements is the same in all arrays for the Arrays.equal() method. In lines 14 and 15, the first two arrays are compared and true is returned since its elements are identical.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arrays_equals.asp
Java Arrays equals() Method
Java Examples Java Videos Java ... = {"Volvo", "BMW", "Tesla"}; System.out.println(Arrays.equals(cars, cars2)); ... The equals() method checks whether two arrays are equal....
๐ŸŒ
admin
how2do.co.in โ€บ home โ€บ array โ€บ 1.how to check if two arrays contain the same elements java.
1.How to check if two arrays contain the same elements java.
September 9, 2024 - This program defines a static method areEqualArrays that takes in two integer arrays and returns true if they contain the same elements, and false otherwise. The method first checks if the arrays have the same length.
Top answer
1 of 6
24

Do you care about duplicate counts? For example, would you need to distinguish between { 1, 1, 2 } and { 1, 2, 2 }? If not, just use a HashSet:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1));
    HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(arr2));
    return set1.equals(set2);
}

If you do care about duplicates, then either you could use a Multiset from Guava.

If you want to stick with the sorting version, why not use the built-in sorting algorithms instead of writing your own?

EDIT: You don't even need to create a copy, if you're happy modifying the existing arrays. For example:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}

You can also have an optimization for the case where the arrays aren't the same length:

public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
    // TODO: Null validation...
    if (arr1.length != arr2.length) {
        return false;
    }
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}
2 of 6
4

If you have no duplicates you can turn the arrays into sets:

new HashSet<Integer>(Arrays.asList(arr1))
    .equals(new HashSet<Integer>(Arrays.asList(arr2)))

Otherwise:

List<Integer> l1 = new ArrayList<Integer>(Arrays.asList(arr1));
List<Integer> l2 = new ArrayList<Integer>(Arrays.asList(arr1));

Collections.sort(l1);
Collections.sort(l2);

l1.equals(l2);
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-check-if-two-arrays-are-equal-or-not
How to Check If Two Arrays are Equal or Not in Java
The Arrays.equals() method compares the actual contents of two arrays. It returns true if both arrays are equal, meaning they have the same elements in the same order. The following is the syntax for java.util.Arrays.equals() method ยท public ...
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-arrays-equals-method-explained-271f9420177d
Javaโ€™s Arrays.equals() Method Explained | Medium
September 11, 2024 - The Arrays.equals() method steps in to address this problem. Instead of comparing the references, it checks if both arrays have the same length and whether each corresponding element is equal.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ compare-two-arrays-java
Compare Two Arrays in Java - GeeksforGeeks
July 23, 2025 - // Java Program to Check If Two Arrays Are Equal // Using equals() method of Arrays class import java.util.Arrays; class GFG { public static void main(String[] args) { // Declaring and initializing two integer arrays // having same values int ...
Find elsewhere
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ dsa in java โ€บ check if two arrays are same or not
Check if two arrays are same or not | PrepInsta | Top 100 Codes
May 1, 2021 - Step 3. Sort both arrays. Step 4. Initialise the for loop from 0 to length for check the element is same or not.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ comparing arrays in java
Comparing Arrays in Java | Baeldung
January 8, 2024 - On the other hand, to check if two arrays are equal in terms of their contents, Java provides the Arrays.equals static method. This method will iterate through the arrays, per position in parallel, and apply the == operator, for every pair of ...
๐ŸŒ
Java Concept Of The Day
javaconceptoftheday.com โ€บ home โ€บ how to check the equality of two arrays in java?
How To Check The Equality Of Two Arrays In Java?
November 11, 2016 - This method takes two arrays as parameters and returns true if both the arrays have same number of elements and corresponding pairs of elements of both arrays are equal. class EqualityOfTwoArrays { public static void main(String[] args) { String[] s1 = {"java", "j2ee", "struts", "hibernate"}; String[] s2 = {"jsp", "spring", "jdbc", "hibernate"}; String[] s3 = {"java", "j2ee", "struts", "hibernate"}; System.out.println(Arrays.equals(s1, s2)); //Output : false System.out.println(Arrays.equals(s1, s3)); //Output : true } }
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ finding-matching-items-in-two-java-arrays-43751484730f
Finding Matching Items in Two Java Arrays | Medium
July 17, 2025 - The most direct way to find matching values between two arrays is to check each item in one against every item in the other. Itโ€™s the first thing most developers write without reaching for libraries or extra tools.
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ check for array equality in java
Check for array equality in Java | Techie Delight
July 7, 2026 - Java Collections framework provides Arrays.equals() utility method for comparing arrays for equality. This method compares two arrays of primitive types or objects by iterating over each element and using the equals() method of the corresponding ...
๐ŸŒ
MindStick
mindstick.com โ€บ forum โ€บ 157544 โ€บ write-a-java-program-to-check-if-two-arrays-contain-the-same-elements
Write a Java program to check if two arrays contain the same elements. โ€“ MindStick
March 23, 2023 - The code given below checks if two integer arrays are the same or not. public class equal { public static void main(String[] args) { // Initialize the first array int a[] = { 10, 30, 11 }; // Initialize the second array int b[] = { 10, 30, 12 }; int flag = 1; // Check if the elements are same or not if (a.length == b.length) { for (int i = 0; i < a.length; i = i + 1) { if (a[i] != b[i]) { flag=0; break; //if the elements are different, set flag as 0 and break out of the loop } } } else { flag = 0; } if (flag==1) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } } OUTPUT: Arrays are not equal
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ equals
Java Array equals()
The Arrays.equals() method in Java is a utility function provided by the java.util.Arrays class. It is used to compare two arrays for equality. This method checks whether two arrays are equal by comparing the elements in both arrays in sequence. It is overloaded to handle arrays of different ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
#1. Java Program To Check Equality Of Two Arrays - YouTube
We will write Java code to find if two one dimensional arrays are equal. For this problem, two arrays will be equal if:-1. Lengths of both arrays are the sa...
Published: January 14, 2022
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ compare
Java Array compare()
Since both arrays contain the same elements in the same order, the method returns true. import java.util.Arrays; public class CompareMultidimensionalArrays { public static void main(String[] args) { int[][] array1 = {{1, 2}, {3, 4}}; int[][] array2 = {{1, 2}, {3, 4}}; boolean isEqual = Arrays.dee...
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ checking whether two arrays are equal or contain mismatches
Checking Whether Two Arrays Are Equal or Contain Mismatches
February 25, 2020 - Two arrays of Object are considered equal based on the equals() contract, or based on the specified Comparator . We can easily check whether melons1 is equal to melons2 , and whether melons1 is equal to melons3 as follows: ... While these examples ...