Do you care about duplicate counts? For example, would you need to distinguish between { 1, 1, 2 } and { 1, 2, 2 }? If not, just use a HashSet:
public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1));
HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(arr2));
return set1.equals(set2);
}
If you do care about duplicates, then either you could use a Multiset from Guava.
If you want to stick with the sorting version, why not use the built-in sorting algorithms instead of writing your own?
EDIT: You don't even need to create a copy, if you're happy modifying the existing arrays. For example:
public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
You can also have an optimization for the case where the arrays aren't the same length:
public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
// TODO: Null validation...
if (arr1.length != arr2.length) {
return false;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
Answer from Jon Skeet on Stack OverflowDo you care about duplicate counts? For example, would you need to distinguish between { 1, 1, 2 } and { 1, 2, 2 }? If not, just use a HashSet:
public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1));
HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(arr2));
return set1.equals(set2);
}
If you do care about duplicates, then either you could use a Multiset from Guava.
If you want to stick with the sorting version, why not use the built-in sorting algorithms instead of writing your own?
EDIT: You don't even need to create a copy, if you're happy modifying the existing arrays. For example:
public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
You can also have an optimization for the case where the arrays aren't the same length:
public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
// TODO: Null validation...
if (arr1.length != arr2.length) {
return false;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
If you have no duplicates you can turn the arrays into sets:
new HashSet<Integer>(Arrays.asList(arr1))
.equals(new HashSet<Integer>(Arrays.asList(arr2)))
Otherwise:
List<Integer> l1 = new ArrayList<Integer>(Arrays.asList(arr1));
List<Integer> l2 = new ArrayList<Integer>(Arrays.asList(arr1));
Collections.sort(l1);
Collections.sort(l2);
l1.equals(l2);
You can do in a single loop with Arrays.equals() method.
public static boolean checkIdentical(int[][] targetArray) {
int[] prev = null;
for (int[] a : targetArray) {
if (prev != null && !Arrays.equals(a, prev))
return false;
prev = a;
}
return true;
}
need to import java.util.Arrays.
Probably try something like this:
public static boolean checkIdentical(int[][] targetArray) {
for (int i = 1; i < targetArray.length; i++) {
for (int j = 0; j < targetArray[i].length; j++) {
if (targetArray[i][j] != targetArray[0][j]) {
return false;
}
}
}
return true;
}
Caveat:
If the arrays can have variable lengths like this:
int[][] identicalArray = {{3, 3, 3, 4}, {3, 3, 3}};
Then the condition will be:
if (targetArray[i].length != targetArray[0].length
|| targetArray[i][j] != targetArray[0][j]) {
return false;
}