Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain the same set of elements and in the same order.
Note: If there are repetitions, then counts of repeated elements should be the same for two arrays to be equal.
Examples:
Input : arr1[] = {1, 2, 5, 4, 0};
arr2[] = {1, 2, 5, 4, 0};
Output : Yes
Input : arr1[] = {1, 2, 5, 4, 0, 2};
arr2[] = {2, 4, 5, 0};
Output : No
Input : arr1[] = {1, 7, 7};
arr2[] = {7, 7, 1};
Output : No
Method 1: Using the pre-defined method
- First, we will initialize two arrays and will insert the elements in both the arrays.
- After that, Arrays.equal() function is called to check whether the two arrays are equal or not and the result will be stored into one boolean variable namely result.
- Finally, the result will be printed.
Example: Below is the implementation of the above approach.
Java
import java.util.Arrays;
public class CheckArraysEqual {
public static void main(String[] args)
{
int a[] = { 30 , 25 , 40 };
int b[] = { 30 , 25 , 40 };
boolean result = Arrays.equals(a, b);
if (result == true ) {
System.out.println( "Two arrays are equal" );
}
else {
System.out.println( "Two arrays are not equal" );
}
}
}
|
Output
Two arrays are equal
Example 2:
Java
import java.util.Arrays;
public class CheckArraysEqual {
public static void main(String[] args)
{
int a[] = { 30 , 25 , 40 , 23 };
int b[] = { 30 , 26 , 40 };
boolean result = Arrays.equals(a, b);
if (result == true ) {
System.out.println( "Two arrays are equal" );
}
else {
System.out.println( "Two arrays are not equal" );
}
}
}
|
Output
Two arrays are not equal
Time Complexity : O(n)
Auxiliary Space : O(1)
Method 2: Without using pre-defined function
- First, we will initialize two arrays a and b and insert the elements in both the arrays. Then create a boolean variable called result to store the result after checking.
- Then we will check the length of the arrays whether the length of the arrays are equal or not.
- If it is equal then, loop will repeat for every element till the end of the array. If somewhere some element is not equal then we will make the result as false.
- If the value of the result variable is false, it means the arrays are not equal.
- If the arrays are equal then the value of the result variable will remain true.
- And also if the length of the arrays is not equal it will return false.
Example 1: Below is the implementation of the above approach.
Java
public class checkArraysEqual {
public static void main(String[] args)
{
int a[] = { 10 , 30 , 12 };
int b[] = { 10 , 30 , 12 };
boolean result = true ;
if (a.length == b.length) {
for ( int i = 0 ; i < a.length; i = i + 1 ) {
if (a[i] != b[i]) {
result = false ;
}
}
}
else {
result = false ;
}
if (result == true ) {
System.out.println( "Arrays are equal" );
}
else {
System.out.println( "Arrays are not equal" );
}
}
}
|
Example 2:
Java
public class checkArraysEqual {
public static void main(String[] args)
{
int a[] = { 10 , 30 , 12 };
int b[] = { 45 , 50 , 55 , 60 , 65 };
boolean result = true ;
if (a.length == b.length) {
for ( int i = 0 ; i < a.length; i = i + 1 ) {
if (a[i] != b[i]) {
result = false ;
}
}
}
else {
result = false ;
}
if (result == true ) {
System.out.println( "Arrays are equal" );
}
else {
System.out.println( "Arrays are not equal" );
}
}
}
|
Output
Arrays are not equal
Time Complexity : O(n)
Auxiliary Space : O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Oct, 2020
Like Article
Save Article