how to write a Java program that checks if two arrays contain the same elements?
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.
Steps to compare two arrays in Java?
- Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compares using the equals method.
- Using Arrays. deep Equals(array1, array2) methods − This method iterates over each value of an array and deep compares using any overridden equals method.
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.
Steps to compare two arrays in Java?
- Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compares using the equals method.
- Using Arrays. deep Equals(array1, array2) methods − This method iterates over each value of an array and deep compares using any overridden equals method.
To write a Java program that checks if two arrays contain the same elements, you can follow these steps:
- Define a method, let’s call it
arraysEqual, that takes two arrays as parameters and returns a boolean value indicating whether the arrays have the same elements. - Inside the
arraysEqualmethod, check if the lengths of the two arrays are equal. If not, returnfalseimmediately, as arrays with different lengths cannot contain the same elements. - Sort both arrays using the
Arrays.sort()method to ensure that the elements are in the same order. - Use a loop to iterate through each element of the arrays and compare them. If any elements are different, return
false. Otherwise, if all elements are the same, returntrue.
Here’s the complete code example:
<span class="hljs-keyword">import</span> java.util.Arrays;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArrayEqualityChecker</span> {
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-type">int</span>[] array1 = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>};
<span class="hljs-type">int</span>[] array2 = {<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>};
<span class="hljs-type">int</span>[] array3 = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>};
<span class="hljs-type">boolean</span> <span class="hljs-variable">isEqual</span> <span class="hljs-operator">=</span> arraysEqual(array1, array2);
System.out.println(<span class="hljs-string">"array1 and array2 are equal: "</span> + isEqual);
isEqual = arraysEqual(array1, array3);
System.out.println(<span class="hljs-string">"array1 and array3 are equal: "</span> + isEqual);
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">arraysEqual</span><span class="hljs-params">(<span class="hljs-type">int</span>[] array1, <span class="hljs-type">int</span>[] array2)</span> {
<span class="hljs-keyword">if</span> (array1.length != array2.length) {
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
<span class="hljs-comment">// Sort the arrays</span>
Arrays.sort(array1);
Arrays.sort(array2);
<span class="hljs-comment">// Compare each element</span>
<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i < array1.length; i++) {
<span class="hljs-keyword">if</span> (array1[i] != array2[i]) {
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}
}
In this example, we have two arrays: array1 and array2. We call the arraysEqual method with these arrays to check if they have the same elements. The output will be:
array1 <span class="hljs-keyword">and</span> array2 <span class="hljs-keyword">are</span> equal: <span class="hljs-literal">true</span>
array1 <span class="hljs-keyword">and</span> array3 <span class="hljs-keyword">are</span> equal: <span class="hljs-literal">false</span>
This indicates that array1 and array2 contain the same elements, while array1 and array3 do not.
