Question: I have this code which is to check in the two arrays and print out the values that dont exist in the other array. I think the way i did it is not the most efficient way to do it hence can anyone offer a better OOP way to write this code in Java? 

Thanks 

```
 public class Calculate {
        static int [] x = {1,2,4,6,7};
        static int [] y = {2,3,4,6,7}; 
        static boolean xflag = true;
        static boolean yflag = true;

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            for(int i = 0; i<x.length; i++)
            {

            for (int b=0;  b<y.length; b++)
                {
                    if(x[i]!= y[b])
                    {
                        xflag= false;

                    }
                    else
                    {
                        xflag = true;
                        break;
                    }

                }

                if(xflag==false)
                {
                    System.out.println(x[i] +" does not exist in array 2");
                }

            }

            for(int i = 0; i<x.length; i++)
            {

            for (int b=0;  b<y.length; b++)
                {
                    if(y[i]!= x[b])
                    {
                        yflag= false;

                    }
                    else
                    {
                        yflag = true;
                        break;
                    }

                }

                if(yflag==false)
                {
                    System.out.println(y[i] +" does not exist in array1");
                }

            }

        }

    }

```

Answer: 
You can code this way

```
List<Integer> array1 = Arrays.asList(1,2,4,6,7);
List<Integer> array2 = Arrays.asList(2,3,4,6,7);

List<Integer> disjointArray = new ArrayList<Integer>();
for (Integer value : array1) {
    if (!array2.contains(value)) {
        disjointArray.add(value);
    }
}

```

And then you can print disjointArray or do whatever manipulation you want.
Here a running example using Javas Collection classes:

```
public class Disjunction {
    public static void main(String args[]) throws UnsupportedEncodingException {
        //Some data preparation
        List<Integer> list1=Arrays.asList(1,2,4);
        List<Integer> list2=Arrays.asList(5,2,8);

        //Here calculating data1-data2 and data2-data1, collect all list items
        //that are in data1 or in data2 but not in both.
        List<Integer> data1 = new ArrayList<>(list1);
        data1.removeAll(list2);
        List<Integer> data2 = new ArrayList<>(list2);
        data2.removeAll(list1);

        //Merging both results. data1 contains now exclusive or of list1 and list2
        data1.addAll(data2);

        System.out.println("exclusive or is " + data1);
    }
}

```

It prints out

```
exclusive or is [1, 4, 5, 8]

```