0

For example we have this kind of list ("A", "B", "C", "D"). We need to compare each one with another, not duplicate comparisons for example output should be like that.

  1. "A" with "B", "C" with "D"
  2. "A" with "C", "B" with "D"
  3. "A" with "D", "B" with "C"

        List<String> list = Arrays.asList("A", "B", "C", "D");
    for (int i = 0; i < list.size(); i++) {
        for (int j = i + 1; j < list.size(); j++) {
            System.out.print(" Compare " + list.get(i) + " " + list.get(j));
        }
        System.out.println();
    }
    

This code return all combination, but not grouped.

Sergey
  • 1
  • You should look into *power set*, and think about limiting the subset to size = 2. – sleepToken Jun 04 '20 at 13:54
  • Does this answer your question? [Obtaining a powerset of a set in Java](https://stackoverflow.com/questions/1670862/obtaining-a-powerset-of-a-set-in-java) – sleepToken Jun 04 '20 at 13:55

4 Answers4

1

You can do solve it as follows:

  1. Create a new List e.g. result as shown in the example below.
  2. Using your nested loops, create a combination of list.get(i) and list.get(j). Then, check if this combination of its reverse is already there in result; if not, add this combination to result.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            List<String> list = Arrays.asList("A", "B", "C", "D");
            List<String> result = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                for (int j = i + 1; j < list.size(); j++) {
                    String combination = list.get(i) + " " + list.get(j);
                    if (!result.contains(combination)
                            && !result.contains(new StringBuilder(combination).reverse().toString())) {
                        result.add(combination);
                    }
                }
            }
    
            // Display the result
            for (String combination : result) {
                System.out.println("Compare " + combination);
            }
        }
    }
    

    Output:

    Compare A B
    Compare A C
    Compare A D
    Compare B C
    Compare B D
    Compare C D
    
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

To achieve your "grouping" you can compare A and B then remove them from the list, and compare the remaining which will be C and D. Then compare A and C, remove them and the compare the remaining which will be B and D. and etc.

You can do this recursively for any list only if the number of elements is even, because after removing 2 elements from the list, you want the remaining number of elements to be a multiple of 2, or you will be left with 1 element at the end

ELA
  • 11
  • 5
0

Here is something you can do , compare each element of the list with all the elements , and add the two to a list of String , to prevent duplications ( it will see if these 2 exist already before comparing them ) :

List<String> list = Arrays.asList("A", "B", "C", "D");
List<String> existing = new ArrayList<String>();

for ( String element : list ) {
    for ( String element2 : list ) {
        if (!existing.contains(element+element2)) {
             System.out.println(" Compare "+element+" with "+element2);
             existing.add(element+element2);
           }
    }
}
Karam Mohamed
  • 843
  • 1
  • 7
  • 15
0

Effectively, you are looking into the tournament scheduling problem. Comparison is a match, and grouping forms a round. There are many ways to do so, and you may start here.

user58697
  • 7,808
  • 1
  • 14
  • 28