Finding # of occurrences of fractions in Java

I am trying to find and print the number of occurrences of fractions from a file in my program. Fractions that simplify down to the same number counts as an occurrence, so 12/6 counts for 6/3 as well. So far, I have separated the fractions into numerator and denominator in separate arrays. The fractions I have in the numerator and denominator came from a separate array that I received from a file. I am having trouble trying to figure out how to simplify the fractions and also find the total occurrence of them. This is what I have so far:

String[] split = new String[2]; //String that holds numerator and denominator
    int[] numerator = new int[100];
    int[] denominator = new int[100];

    for(int i = 0; i < numOfFractions; ++i) { //Loop through # of Lines
        split = fractions[i].split("/");  //Split the fractions at the /
        System.out.println("Test here " + fractions[i]);  //TODO --> test
        numerator[i] = Integer.parseInt(split[0]);  //Numerator
        System.out.println("Numerator = " + numerator[i]);  //TODO --> test
        denominator[i] = Integer.parseInt(split[1]);  //Denominator
        System.out.println("Denominator = " + denominator[i] + "\n");    //TODO --> test
    }

}

These are the fractions obtained from a file. Each fraction is on its own line, and can assume every fraction will be (A/B) format

6/3
4/2
5/9
80/90
800/900
5/5
1/2
1/3
1/1
1/4
2/7
2/8
2/9

Solution 1:

You can reduce them by finding the greatest common divisor of the numerator and the denominator. You can do it like so.

public static int gcd(int r, int s) {
    while (s > 0) {
        int t = r % s;
        r = s;
        s = t;
    }
    return r;
}

for 27/12

int d = gcd(27,12); // returns 3

27/3 = 9
12/3 = 4

so the reduced fraction is 9/4. So every equal fraction will reduce to the same value when the numerator and denominator are divided by the GCD of those numbers.

It uses Euclid's Algorithm

Here is how it might be applied in your case.

  • create a map to hold the reduced fraction as key and an Integer for the count.
  • then convert the fraction to a numeric value and divide by the GCD
  • then store the reduced fraction as a string key and increment the count for that key.
    
String[] s = {"10/2", "27/3", "19/4",  "15/3", "12/4",  "3/5", "9/15"};
    
Map<String, Integer> fractions = new HashMap<>();
        
        String[] s = { "10/2", "27/3", "19/4", "15/3", "12/4", "3/5",
                "9/15" };
        
        for (String vv : s) {
            String[] nd = vv.split("/");
            int n = Integer.parseInt(nd[0]);
            int d = Integer.parseInt(nd[1]);
            int div = gcd(n, d);
            n /= div;
            d /= div;
            String reduced = String.format("%d/%d", n, d);
            fractions.compute(reduced, (k,v)->v == null ? 1 : v + 1);
                
        }
        fractions.entrySet().forEach(System.out::println);
}

prints

3/1=1
5/1=2
3/5=2
19/4=1
9/1=1

Note. What
fractions.compute(reduced, (k,v)->v == null ? 1 : v + 1);
does is to check if the value is null. If so, set the count to 1, otherwise add 1 to the existing count.