Home > database >  Display the combinations and the number of combinations without the same value of the two array
Display the combinations and the number of combinations without the same value of the two array

Time:03-20

I need to create a function that will accept two arrays of integers. Display the combinations and the number of combinations without the same value.

Example:

Array 1 = { 1, 2, 3 }; 
Array 2 = { 2, 3, 4 };

Output :

1,2
1,3
1,4
2,3
2,4
3,2
3,4

Combination count : 7

I've seen a lot of complicated codes here and can't find the exact goal that I am looking for so far, While waiting for someone to help I will just keep browsing and searching. Also I'm using Java 7.

CodePudding user response:

Try this.

Note: This code is considering given example. There are no validation done. You can add it if needed.

int count = 0;

        for (int i = 0; i < array.length; i  ) {
            for (int j = 0; j < array2.length; j  ) {
                if(array[i] != array2[j]){
                    System.out.println("Combination " array[i] " " array2[j]);
                    count = count  1;
                }
            }
        }
        System.out.println("Combination " count);
  • Related