3

I've tried to print the non-intersection of sets between 2 array A and B. But, I have a problem how to print the elements on A different B. Here is my sample code:

public class Array {

    public static void main(String[] args) {
        for (int i = 0; i <= arrA.length - 1; i++) {
            arrA[i] = sc.nextInt();
        }
        for (int i = 0; i <= arrB.length - 1; i++) {
            arrB[i] = sc.nextInt();
        }

        boolean x = true;
        int y = 0;
        for (int i = 0; i < arrA.length; i++) {
            for (int j = 0; j < arrB.length; j++) {
                if (arrA[i] == arrB[j]) {
                    arrTestA[i] = true;
                }else y = arrA[i];
            }
        }

        for (int i = 0; i < arrA.length; i++) {
            x = x && arrTestA[i];
        }

        if (x) {
            System.out.println("All the elements of A contained in B.");
        }else {
            System.out.println("There are elements on A different B.");
            System.out.println("The elements of A which is not in B = "); //My Problem
        }

    }
}
2
  • 1
    Possible duplicate of differences between two arrays Commented Dec 1, 2016 at 15:02
  • Thank you. I thinks it's no problem. The problem is how I can get 'The elements of A which is not in B = 4' Commented Dec 1, 2016 at 15:26

4 Answers 4

4

To accomplish that you could use Collections and retainAll method. E.g.:

List<Integer> arrTestA = new ArrayList<>();
List<Integer> arrTestB = new ArrayList<>();

[...]

List<Integer> common = new ArrayList<>(arrTestA);
common.retainAll(arrTestB);

List<Integer> diff = new ArrayList<>();
for(Integer element : arrTestA) 
    if(!common.contains(element))
        diff.add(element);

[here you print out elements of diff as The elements of A which is not in B]

ETA: Non Collection attempt:

int[] arr1 = { 1, 11, 5, 9, 4, 3, 4, 8 };
int[] arr2 = { 1, 7, 5, 3, 4, 8 };

Arrays.sort(arr1);
Arrays.sort(arr2);

for (int i : arr1) {
    boolean contains = false;
    for (int j : arr2) {
        if (i == j) {
            contains = true;
            break;
        }
    }
    if (!contains)
        System.out.println("Arr2 doesn't contain number: " + i);
}

...or the loop can look like this:

outer: for (int i : arr1) {
    for (int j : arr2) {
        if (i == j) {
            continue outer;
        }
    }
    System.out.println("Arr2 doesn't contain number: " + i);
}

This is only one way method, but hope you see the point.

ETA2: In my approach, in fact, these Arrays don't have to be sorted. You can simply delete lines of code that are responsible for sorting.

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

4 Comments

Thank you. How if I don't use array list, collection, and retainAll. I just want to use the control structure like selection and looping, if and for
Thank you. How if the code doesn't use method like Arrays.sort() anymore. The algorithm only use selection and loop. @user629735
In my approach, in fact, these Arrays don't have to be sorted. You can simply delete lines of code that are responsible for sorting.
Oh I see... Thank you so much @user629735
3

You can use a Set (it requires additional space to store its elements, but the code is simple):

Integer[] a = {0, 1, 2};
Integer[] b = {1, 2, 3};
Set<Integer> setFromA = new HashSet<>(Arrays.asList(a));
for (int num : b) {
    if (!setFromA.contains(num)) {
        System.out.println(num);
    }
}

The same using Java 8 Stream API:

Arrays.stream(b).filter(num -> !setFromA.contains(num)).forEach(System.out::println);

Also, you can save result into a new list (if you wish):

List<Integer> result = Arrays.stream(b)
        .filter(num -> !setFromA.contains(num))
        .collect(Collectors.toList());

Comments

1

The idea is to sort the two arrays and go through both of them. By sorting them you would not have to cross-check all elements

   int[] arr1 = {1,3,5,6,7,4,8};
   int[] arr2 = {1,2,5,6,9,4,8};

    Arrays.sort(arr1);
    Arrays.sort(arr2);
    int j =0;
    for(int i = 0; i < arr1.length; i++){
        while(j < arr2.length && arr2[j] < arr1[i]){
            System.out.println("number: " +arr2[j]);
            j++;
        }

        if(arr2[j] != arr1[i]){
            System.out.println("number: " +arr1[i]);
        }else{
            j++;
        }
    }

Comments

1

I would not suggest using list here because contains takes o(n) time complexity.
Use set as its contains take constant time:(In case you want to maintain order also, go for LinkedHashSet)

// store array b into a set
Set<Integer> setInts = new HashSet<>(Arrays.asList(B);

// traverse array a, and check if set contains currrent element
for(int e : A)
{
   if(!setInts.contains(e))
     System.out.println(e);
}

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.