Hi and hope someone can help. My method seems to work but when I submit it it fails in some respects. When I check it with the values that it says it fails on, it works so I'm not sure what's going on.
Thanks for your interest.
Here's my code
public static void smartCombine(ArrayList<Integer> list1, ArrayList<Integer> list2) {
ArrayList<Integer> tempList = new ArrayList<Integer>();
tempList.addAll(list2); // Copies list2 values into tempList
// Next line is mainly for checking errors before anything is done
System.out.println("List1: " + list1 + "\tList2: " + list2 + "\ttempList: " + tempList);
for (int i : list2) {
System.out.println("i: " + i);
if (list1.contains(i)) {
tempList.remove(i); // Removes an element from tempList if it already exists in list1
}
}
list1.addAll(tempList); // Copies the non duplicated values from tempList into list1
}
It tells me it fails with the following but when I hard code these values into the main method it works just fine:-
Combination of lists [10, 11] and [5] should contain the number 5.
Combination of lists [5, 1, 2] and [40] should contain the number 40.
Combination of lists [10, 11, 12, 13] and [5, 6, 7, 8, 9] should contain the number 5.