0

I'm trying to write a method that takes 2 ArrayLists of doubles and returns all the values in set1 that aren't found in set2. These numbers should be returned in set3. Normally I would just use set.contains but I can only use set.get set.size and set.add. Can anyone point me in the right direction?

For example:

If set1 has the numbers 1,2,3,4,5

and set2 has the numbers 1,7,9,5,3

set3 should only contain 2,4,5

ArrayList<Double> setDiff(ArrayList<Double> set1, ArrayList<Double> set2){
    ArrayList<Double> set3 = new ArrayList<Double>();
    for(int i = 0; i < set1.size(); i++){
        for(int x = 0; x < set2.size(); x++){
            if(set1.get(i) != set2.get(x)){
                set3.add(set1.get(i));
            }
        }
    }
    return set3;
}
3

3 Answers 3

3

Problem is, you are adding the number to set3 based on the first failure. So, if the first element of set2 does not matches the current element of set1, you are adding it to set3.

I suspect that this is your homework, given the restrictions in your toolkit. So, I'll just give you an idea of how to approach.

You can make use of a boolean variable, and toggle it (for e.g. set it to false), as soon as you find the current element in set2, and then break out of the inner loop.

So, your condition in the inner loop will change from: -

if(set1.get(i) != set2.get(x))

to: -

if(set1.get(i) == set2.get(x)) {
    // The current element in set1 is present in set2. 
    // toggle the boolean variable
    // break out of loop. As you no more want to check for further elements.
}

Then outside the inner loop, check the status of that boolean variable. And based on the status, you can add or not add the current item to set3. for e.g. If the boolean variable is false, that means you found the element in set2, so don't add it to set3, else add it.

You also need to reset the boolean variable at the start of the outer loop each time.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try using Apache commons CollectionUtils.removeAll method :this method returns a collection containing all the elements in c that are not in remove.

ArrayList commonList = CollectionUtils.removeAll(arrayList1, arrayList2);

Comments

0

try

static ArrayList<Double> setDiff(ArrayList<Double> set1, ArrayList<Double> set2) {
    ArrayList<Double> copy1 = (ArrayList<Double>) set1.clone();
    copy1.retainAll(set2);
    ArrayList<Double> copy2 = (ArrayList<Double>) set1.clone();
    copy2.removeAll(copy1);
    return copy2;
}

public static void main(String[] args) throws Exception {
    ArrayList l1 = new ArrayList(Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0));
    ArrayList l2 = new ArrayList(Arrays.asList(1.0, 7.0, 9.0, 5.0, 3.0));
    System.out.println(setDiff(l1, l2));
}

prints

[2.0, 4.0]

Besides, I would suggest to change the method signature as

<T> List<T> listDiff(List<T> list1, List<T> list2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.