I've implemented a method for finding the difference between two unsorted arrays. Currently I have achieved getting the diff without duplicates. But how to make it take duplicates into consideration as well?
For example for the below input arrays I expect the output [4 5 3]:
int[] arr1 = {1, 2, 3, 4, 5, 5};
int[] arr2 = {1, 2, 3, 5, 3};
For these input arrays I expect [7 7 9]
int[] arr3 = {7, 7, 4, 9, 6};
int[] arr4 = {4, 6};
//
static ArrayList<Integer> findDifference(int[] a, int[] b) {
ArrayList<Integer> arr1 = new ArrayList<Integer>() {
{ for (int i : a) add(i); }
};
ArrayList<Integer> arr2 = new ArrayList<Integer>() {
{ for (int i : b) add(i); }
};
if (arr1.size() > arr2.size()) {
arr1.removeAll(arr2);
return arr1;
} else {
arr2.removeAll(arr1);
return arr2;
}
}
difference? Can you provide more examples?4(in arr1 and not in arr2) and3(in arr2 and not in arr1) - then you need a combination of both andremoveAll()and a simplereturnof one array wont work -- as you are missing the data from the other