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
        }

    }
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
Mark Martin
  • 57
  • 2
  • 13
  • 1
    Possible duplicate of [differences between two arrays](http://stackoverflow.com/questions/13732312/differences-between-two-arrays) – Arnaud Dec 01 '16 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' – Mark Martin Dec 01 '16 at 15:26

4 Answers4

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.

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
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());
Oleksandr Pyrohov
  • 14,685
  • 6
  • 61
  • 90
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++;
        }
    }
Aimee Borda
  • 842
  • 2
  • 11
  • 22
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);
}
Bhagwati Malav
  • 3,349
  • 2
  • 20
  • 33