Skip to content

Commit ce7043c

Browse files
Create sorting_array.java
1 parent faee099 commit ce7043c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

sorting_array.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Arrays;
2+
3+
public class ArrayEquality {
4+
public static boolean areArraysEqual(int[] array1, int[] array2) {
5+
int length1 = array1.length;
6+
int length2 = array2.length;
7+
8+
// If lengths of arrays are not equal, they are not equal
9+
if (length1 != length2) {
10+
return false;
11+
}
12+
13+
// Sort both arrays
14+
Arrays.sort(array1);
15+
Arrays.sort(array2);
16+
17+
// Linearly compare elements
18+
for (int i = 0; i < length1; i++) {
19+
if (array1[i] != array2[i]) {
20+
return false;
21+
}
22+
}
23+
24+
// If all elements are the same
25+
return true;
26+
}
27+
28+
public static void main(String[] args) {
29+
int[] array1 = {3, 5, 2, 5, 2};
30+
int[] array2 = {2, 3, 5, 5, 2};
31+
32+
if (areArraysEqual(array1, array2)) {
33+
System.out.println("The arrays are equal");
34+
} else {
35+
System.out.println("The arrays are not equal");
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)