Related Tags

array
java
communitycreator

# How to check if two arrays contain the same elements in Java Harsh Jain

Two arrays contain the same elements when the number of elements as well as the values of the elements are identical in both arrays.

Note: We need to identify the similarity in the elements themselves, not the similarity in the order of elements of the two arrays.

### Example

Let’s understand with the help of an example.

Suppose that the first array contains [1, 3, 4, 5, 7] and the second array contains [1, 7, 4, 5, 3]. The two arrays contain the same elements since their size is the same and the data elements are also identical.

Another example could be that the first array contains [1, 3, 4, 5, 7] and the second array contains [1, 6, 2, 5, 3]. The two arrays do not contain the same elements since even though their size is the same, some of the elements differ.

### Solution approach

We will be using two methods: Arrays.sort() and Arrays.equals() from the java.util package to solve this problem.

• The Arrays.sort() method sorts the elements of the array in increasing order. This method will accept the array which needs to be sorted and return the sorted array.

• The Arrays.equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

### Code

The code for the algorithm above is provided.

import java.util.*;
class Main
{
public static void main(String[] args)
{
Object[] a = new Object[] {1, 5, 7, 9};
Object[] b = new Object[] {9, 5, 1, 7};
Object[] c = new Object[] {1, 6, 7, 5};

Arrays.sort(a);
Arrays.sort(b);
Arrays.sort(c);

boolean returnVal1 = Arrays.equals(a,b);
System.out.println("Array a and b equal?: " + returnVal1);

boolean returnVal2 = Arrays.equals(b,c);
System.out.println("Array b and c equal?: " + returnVal2);
}
}

Check whether all the elements in two arrays are same or not in Java

### Explanation

• In line 1 we imported the required package.

• In line 2 we made a class called Main.

• In lines 6 to 8 we initialized three object arrays which we want to compare.

• 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.

• However, when the last two arrays are compared in lines 17 and 18, false is returned since some of the elements are different even though the size is the same.

This is how we can check whether the two arrays contain the same elements in Java.

Note: If you also want to check the similarity based on the array order then do not sort the arrays. Instead, directly pass the arrays to the Arrays.equals() method.

RELATED TAGS

array
java
communitycreator

CONTRIBUTOR Harsh Jain
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 