How to Compare Two Arrays in Java
In this tutorial we compare two arrays in Java, we need to check if they have the same length and if each element in one array matches the corresponding element in the other.
- Check whether the two arrays' length is the same.
- If they have different lengths, they are not equal.
- Iterate the elements of both arrays and compare them one by one.
- If you find a pair of elements that don't match at any point, the arrays are not equal.
- If you've checked all the elements in both arrays and they all match, then the arrays are equal.
Note that the comparison must take into account the order of the elements in the arrays. If the order doesn't matter, you can sort the arrays first before comparing them.
Approach: By Using Arrays.equals() Method
We can compare two arrays in Java using the Arrays.equals() method. The Arrays.equals() method provides a convenient and efficient way to compare arrays for equality in Java. It handles the comparison of elements and the length check internally, saving you from writing custom loops or iteration logic.
FileName: ArrayComparisonExample.java
import java.util.Arrays; public class ArrayComparisonExample { public static void main(String[] args) { // Initialize two arrays with the same elements int[] array1 = {5, 4, 3, 2, 1}; int[] array2 = {5, 4, 3, 2, 1}; // Compare the two arrays using Arrays.equals() method if (Arrays.equals(array1, array2)) { System.out.println("The arrays are equal"); // Print a message if the arrays are equal } else { System.out.println("The arrays are not equal"); // Print a message if the arrays are not equal } } }
Output:
The arrays are equal
Complexity:
The time complexity of this code is O(n), where n is the length of the arrays array1 and array2. It is because the Arrays.equals() method compares the elements of the arrays in a sequential manner, and in the worst-case scenario, it needs to compare all the elements of both arrays.
The space complexity of this code is O(1). It does not require any additional space that grows with the input size. The space used is constant and independent of the length of the arrays.
Approach: By Using Arrays.deepEquals() Method
Here, in this method performs a deep comparison of the elements in the arrays. If we have to deal with arrays of reference types (e.g., arrays of objects), we can use the Arrays.deepEquals() method to compare them.
FileName: ArrayComparison.java
import java.util.Arrays; public class ArrayComparison { public static void main(String[] args) { // Initialize two 2D Integer arrays with the same elements Integer[][] array1 = { {1, 2}, {3, 4}, {5, 6} }; Integer[][] array2 = { {1, 2}, {3, 4}, {5, 6} }; // Compare the two arrays using Arrays.deepEquals() method if (Arrays.deepEquals(array1, array2)) { System.out.println("The arrays are equal"); // Print a message if the arrays are equal } else { System.out.println("The arrays are not equal"); // Print a message if the arrays are not equal } } }
Output:
The arrays are equal
Approach: By Using a Loop
We can use a loop to iterate over the elements of both arrays and compare them one by one. If all the elements match, the arrays are considered equal.
FileName: ArrayComparisonExample.java
public class ArrayComparisonExample { // Define a static method to compare two int arrays public static boolean compareArrays(int[] array1, int[] array2) { // Check if the two arrays have the same length if (array1.length != array2.length) { return false; // If not, return false } // Iterate over the arrays and compare each element for (int i = 0; i < array1.length; i++) { if (array1[i] != array2[i]) { return false; // If any element is not equal, return false } } // If all elements are equal, return true return true; } // Define the main method to test the compareArrays() method public static void main(String[] args) { int[] array1 = {5, 4, 3, 2, 1}; int[] array2 = {5, 4, 3, 2, 1}; // Call the compareArrays() method and print the result if (compareArrays(array1, array2)) { System.out.println("The arrays are equal"); } else { System.out.println("The arrays are not equal"); } } }
Output:
The arrays are equal
Complexity:
The time complexity of Arrays.deepEquals() is O(n), where n is the total number of elements in the arrays. It needs to compare each element of the arrays to determine equality. In this case, as both arrays have the same dimensions and contain the same elements, the method returns true, indicating that the arrays are equal.
The space complexity is also O(n) because the Arrays.deepEquals() method needs to traverse and compare all the elements in the arrays. In this specific case, the space complexity is O(1) because the arrays are small and have a fixed size.
Approach: By Using Arrays.compare() Method
Starting from Java 9, you can use the Arrays.compare() method to compare two arrays. In this method compares the elements of the arrays lexicographically, which means it compares the elements pairwise until a mismatch is found.
FileName: ArrayComparisonExample.java
import java.util.Arrays; public class ArrayComparisonExample { // Define a static method to compare two int arrays using Arrays.compare() method public static int compareArrays(int[] array1, int[] array2) { return Arrays.compare(array1, array2); // Return the comparison result } // Define the main method to test the compareArrays() method public static void main(String[] args) { int[] array1 = {5, 4, 3, 2, 1}; int[] array2 = {5, 4, 3, 2, 1}; // Call the compareArrays() method and print the result int result = compareArrays(array1, array2); if (result == 0) { System.out.println("The arrays are equal"); } else if (result > 0) { System.out.println("The First Array is greater than second array"); } else { System.out.println("Second array is greater than first array"); } } }
Output:
The arrays are equal
Complexity:
The time and space complexity of the Arrays. compare() method used in the compareArrays() method has a time complexity of O(n), where n is the length of the arrays being compared. This is because it needs to compare each element of the arrays to determine the lexicographic order.
Space Complexity: The space complexity of the code is O(1) because it does not use any additional data structures that grow with the input size. The space usage remains constant regardless of the length of the arrays.