Search code examples
javaperformancepoker

get every two card combination from subset of standard deck


I have a standard deck of cards and then have removed a few, from the remaining cards I want to calculate all of the possible two card combinations. For example with 47 cards there is 47 choose 2 combinations. Can anyone think of an efficient way to do this other than

foreach(card){
  combinations.add(card, card +1)
}

Thanks


Solution

  • for(int i=0; i<47; i++) {
      for(int j=i+1; j<47; j++) {
         combinations.add(i, j);
      }
    }
    

    This is the most efficient way, as it goes through each pair only once.

    If you just want the number of two-pair combinations, see here.