-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathsorting_array.java
More file actions
38 lines (31 loc) · 994 Bytes
/
sorting_array.java
File metadata and controls
38 lines (31 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Arrays;
public class ArrayEquality {
public static boolean areArraysEqual(int[] array1, int[] array2) {
int length1 = array1.length;
int length2 = array2.length;
// If lengths of arrays are not equal, they are not equal
if (length1 != length2) {
return false;
}
// Sort both arrays
Arrays.sort(array1);
Arrays.sort(array2);
// Linearly compare elements
for (int i = 0; i < length1; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
// If all elements are the same
return true;
}
public static void main(String[] args) {
int[] array1 = {3, 5, 2, 5, 2};
int[] array2 = {2, 3, 5, 5, 2};
if (areArraysEqual(array1, array2)) {
System.out.println("The arrays are equal");
} else {
System.out.println("The arrays are not equal");
}
}
}