Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the different element of 2 array

Tags:

java

arrays

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
        }

    }
}
like image 855
Mark Martin Avatar asked May 10 '26 13:05

Mark Martin


2 Answers

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.

like image 54
null Avatar answered May 12 '26 04:05

null


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());
like image 45
Oleksandr Pyrohov Avatar answered May 12 '26 02:05

Oleksandr Pyrohov