Skip to content
Advertisement

Count occurrences in 2D Array

I’m trying to count the occurrences per line from a text file containing a large amount of codes (numbers).

Example of text file content:

9045,9107,2376,9017
2387,4405,4499,7120
9107,2376,3559,3488
9045,4405,3559,4499

I want to compare a similar set of numbers that I get from a text field, for example:

9107,4405,2387,4499

The only result I’m looking for, is if it contains more than 2 numbers (per line) from the text file. So in this case it will be true, because:

9045,9107,2376,9017 – false (1)
2387,4405,4499,7120 – true (3)
9107,2387,3559,3488 – false (2)
9045,4425,3559,4490 – false (0)

From what I understand, the best way to do this, is by using a 2d-array, and I’ve managed to get the file imported successfully:

Scanner in = null;
try { 
    in = new Scanner(new File("areas.txt"));
} catch (FileNotFoundException ex) {
    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
List < String[] > lines = new ArrayList < > ();
while ( in .hasNextLine()) {
    String line = in .nextLine().trim();
    String[] splitted = line.split(", ");
    lines.add(splitted);
}

String[][] result = new String[lines.size()][];
for (int i = 0; i < result.length; i++) {
    result[i] = lines.get(i);
}

System.out.println(Arrays.deepToString(result));

The result I get:

[[9045,9107,2376,9017], [2387,4405,4499,7120], [9107,2376,3559,3488], [9045,4405,3559,4499], [], []]

From here I’m a bit stuck on checking the codes individually per line. Any suggestions or advice? Is the 2d-array the best way of doing this, or is there maybe an easier or better way of doing it?

Advertisement

Answer

The expected number of inputs defines the type of searching algorithm you should use.

If you aren’t searching through thousands of lines then a simple algorithm will do just fine. When in doubt favour simplicity over complex and hard to understand algorithms.

While it is not an efficient algorithm, in most cases a simple nested for-loop will do the trick.

A simple implementation would look like this:

final int FOUND_THRESHOLD = 2;

String[] comparedCodes = {"9107", "4405", "2387", "4499"};
String[][] allInputs = {
    {"9045", "9107", "2376", "9017"}, // This should not match
    {"2387", "4405", "4499", "7120"}, // This should match
    {"9107", "2376", "3559", "3488"}, // This should not match
    {"9045", "4405", "3559", "4499"}, // This should match
};

List<String[] > results = new ArrayList<>();
for (String[] input: allInputs) {
    int numFound = 0;

    // Compare the codes
    for (String code: input) {
        for (String c: comparedCodes) {
            if (code.equals(c)) {
                numFound++;
                break; // Breaking out here prevents unnecessary work
            }
        }

        if (numFound >= FOUND_THRESHOLD) {
            results.add(input);
            break; // Breaking out here prevents unnecessary work
        }
    }
}

for (String[] result: results) {
    System.out.println(Arrays.toString(result));
}

which provides us with the output:

[2387, 4405, 4499, 7120]
[9045, 4405, 3559, 4499]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement