Java

How to Check if Two Arrays Contain the Same Elements in Java

In Java programming, there can be instances where there is a requirement for the developer to omit repeated entries in order to streamline the code complexity. For instance, eliminating the identical values at different code locations by sorting them. In such scenarios, checking if two arrays contain identical elements in Java is of great aid in analyzing the repeated and unnecessary values.

This article will elaborate on the approaches to check if two arrays accumulate identical elements in Java.

How to Check if Two Arrays Contain the Same Elements in Java?

To check if two arrays accumulate the same elements in Java, apply the following approaches combined with the “Arrays.sort()” and “Arrays.toString()” methods:

Before proceeding to the approaches, make sure to include the following package to work with “Arrays”:

import java.util.Arrays;

Approach 1: Check if Two Arrays Contain the Same Elements in Java Using the “Array.equals()” Method

The “Arrays.sort()” method can be used to sort an array completely or a part of it by specifying the start and end indexes and the “Arrays.equals()” method checks if the two arrays are equal or not. The “Arrays.toString()” method in Java, however, returns the provided value in the “string” format.

These approaches can be applied in combination to sort the array elements first, transform them into string representation, and then check for identical elements in both arrays.

Syntax

Array.sort(array, start, end);

In the above syntax:

  • array” refers to the array that needs to be sorted.
  • start” is the start index from where to initiate the sorting.
  • end” corresponds to the index where the sorting needs to end.

public static boolean equals(int[] a, int[] a2)

Here, “a” and “a2” point to the arrays that need to be tested for equality.

public static String toString(int[ ] x)

In this syntax:

  • int[ ] x” indicates the array for which the string representation needs to be returned.

Example

Let’s overview the following example:

public class sameelements {
 public static void main(String args[]) {
  int[] array1 = {2, 1, 3};
  int[] array2 = {3, 2, 1};
  System.out.println("The first array is: "+Arrays.toString(array1));
  System.out.println("The second array is: "+Arrays.toString(array2));
  Arrays.sort(array1);
  Arrays.sort(array2);
  System.out.println("The sorted first array is: "+Arrays.toString(array1));
  System.out.println("The sorted second array is: "+Arrays.toString(array2));
  System.out.println("Do the arrays contain the same elements? "
  +Arrays.equals(array1, array2));
}}

In this code, apply the following steps:

  • Firstly, declare two arrays of “int” data types having identical elements but in different order and display them.
  • After that, apply the “sort()” method twice to sort both the declared arrays.
  • Now, apply the “toString()” method to return the sorted arrays in the form of “String”.
  • Lastly, check for the contained same elements in both arrays via the “equals()” method.

Output

In this output, it can be observed that the array elements are sorted and checked appropriately.

Approach 2: Check if Two Arrays Contain the Same Elements in Java Using the “Arrays.deepEquals()” Methods

The “Arrays.deepEquals()” method is utilized to check if two single or multi-dimensional arrays are equal or not. However, in this case, this method can be applied to check upon the one-dimensional string arrays.

Syntax

public static boolean deepEquals(Object[] ob1, Object[] ob2)

In the above-given syntax, “ob1” and “ob2” correspond to the arrays that need to be checked for equality.

Example

Let’s go through the below-provided example:

public class sameelements2 {
 public static void main(String args[]) {
  String[] array1 = {"Harry", "David", "Tim"};
  String[] array2 = {"David", "Tim", "Harry"};
  System.out.println("The first array is: "+Arrays.toString(array1));
  System.out.println("The second array is: "+Arrays.toString(array2));
  Arrays.sort(array1);
  Arrays.sort(array2);
  System.out.println("The sorted first array is: "+Arrays.toString(array1));
  System.out.println("The sorted second array is: "+Arrays.toString(array2));
  boolean result = Arrays.deepEquals(array1, array2);
  System.out.println("Do the arrays contain the same elements? "+result);
}}

According to this code snippet, perform the following steps:

  • First of all, declare two “String” arrays having the stated string values.
  • In the next step, likewise, apply the “toString()” method and display the arrays.
  • Now, similarly, apply the “sort()” method with each of the defined arrays to sort the array elements.
  • Finally, apply the “deepEquals()” method to apply a check upon the identical elements in the arrays, as its argument.

Output

In this outcome, it can be implied that the corresponding outcome against the applied check is returned.

Note: If the check for the identical elements is applied before sorting the array elements, both of the discussed approaches return the “false” outcome.

Conclusion

To check if two arrays accumulate the same elements in Java, apply the “Arrays.sort()” and “Arrays.toString()” methods combined with the “Arrays.equals()”, or the “Arrays.deepEquals()” methods. These approaches can be applied to first, sort the arrays and then return a boolean outcome based on the applied check. This blog demonstrated checking for identical elements in the two arrays.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.