Home > Software engineering >  Java: calculating the amount of pairs in array
Java: calculating the amount of pairs in array

Time:11-22

I have a program ( it is a bonus game for lottery ) that calculates the amount of pairs among generated numbers, it works good, however, I have a problem that - if there is 3 exact numbers it will count that there is already 2 pairs of number, but I need it to only count pairs, not calculate all possible probability to make pairs between numbers:

int randomChance = r.nextInt(100);
for (int newNum = 0; newNum<5 ;newNum  ) {
    int chanceGen = r.nextInt(100);
    if (chanceGen <= 50) {
        bonusGame[newNum] = 10;
    }
    else if (chanceGen <= 77) {
        bonusGame[newNum] = 20;
    }
    else if (chanceGen <= 92) {
        bonusGame[newNum] = 50;
    }
    else if (chanceGen <= 98) {
        bonusGame[newNum] = 200;
    }
    else {
        bonusGame[newNum] = 1000;
    }
    
    }
for (int z: bonusGame) {
    System.out.println(z);
}
for (int f = 0; f < bonusGame.length - 1;   f) {
    for (int j = f   1; j < bonusGame.length;   j) {
        if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 10 ) {
            System.out.println(" You won 10 euro ");
            bonusGameSum  =10;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 20 ) {
            System.out.println(" You won 20 euro ");
            bonusGameSum  =20;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 50 ) {
            System.out.println(" You won 50 euro ");
            bonusGameSum  =50;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 200 ) {
            System.out.println(" You won 200 euro ");
            bonusGameSum  =200;
        }
        else if (bonusGame[j] == bonusGame[f] & bonusGame[j] == 1000 ) {
            System.out.println(" You won 1000 euro ");
            bonusGameSum  =1000;
        }
    }

What should i fix in order for program to calculate right amount of exact pairs?

CodePudding user response:

The following should do it and is even simpler that your nested for solution. You just need to use some Java features (Collections.frequency() for example):

int[] bonusGame = {10, 20, 20, 30, 30, 30, 30, 50, 50, 50, 200};
for (int z: bonusGame) {
    System.out.println(z);
}

List<Integer> bonusGameList = Arrays.stream(bonusGame).boxed().collect(Collectors.toList());
Set<Integer> bonusGameUnique = new HashSet<>(bonusGameList);
Map<Integer, Integer> numbersCount = new HashMap<>();

for (Integer number : bonusGameUnique) {
    int count = Collections.frequency(bonusGameList, number);
    numbersCount.put(number, count);
}

Map<Integer, Integer> numbersPairsCount = numbersCount.entrySet().stream()
        .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue()/2));

System.out.println(numbersPairsCount);

CodePudding user response:

Here's an efficient one-liner:

long numPairs = Arrays.stream(bonusGame).boxed() // stream Integers
  .collect(groupingBy(identity(), counting()))   // collect to map of freqs
  .values().stream()                             // stream frequency counts
  .mapToLong(n -> n / 2)                         // integer halve
  .sum();                                        // sum

It works by first getting the frequencies of all numbers, then sums all frequencies after halving then using integer arithmetic, which discards fractions - ie 5 / 2 results in 2.

As an example, given 10, 20, 10, 30, 10, 50, 10, 50, 30, 30, the result is 4, comprising 2 from 10,10,10,10, 1 from 30,30,30 and 1 from 50,50.

  • Related