1

I am looking for the most common value from the 2D array and how many times it occurs. I tried this solution but it's not working. I tried searching but not able to find a proper example. Please help me solve this problem.

Here is my code:

private void commonElevation(int[][] data) {
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[i].length; j++) {
            if (i + 1 < data.length) {
                if (data[i][j] == data[i + 1][j]) {
                    System.out.println(data[i][j] + " = " + data[i + 1][j]);
                }
            }
        }
    }
}
1
  • For starters, you can't just print in the middle of the loop. For certain, you'll need to at least check every element before prematurely calling something most frequent. Due to all your requirements, the best bet is just to write 4 loops--for every element (outer two loops), count its frequency in the entire grid and compare it to the best (inner two loops). You could use a counter array with a bucket for each integer, but this will destroy the efficiency unless the data is enormous. Commented Sep 24, 2019 at 1:56

2 Answers 2

2

You could use the Stream API:

int[][] data = {{1, 2, 3}, {2, 2, 2}, {4, 5, 6}};

Map<Integer, Long> counts = Arrays.stream(data).flatMapToInt(Arrays::stream).boxed()
        .collect(groupingBy(Function.identity(), counting()));

Optional<Map.Entry<Integer, Long>> max = counts.entrySet().stream().max(Comparator.comparing(Map.Entry::getValue));

max.ifPresent(System.out::println);

Output

2=4

Given the new constraints a brute-force approach will work:

// find the maximum
int value = 0, max = Integer.MIN_VALUE;
for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data[i].length; j++) {

        // search for counts
        int currentCount = 0;
        for (int k = 0; k < data.length; k++) {
            for (int l = 0; l < data[k].length; l++) {
                if(data[k][l] == data[i][j]) {
                    currentCount++;
                }
            }
        }

        if (currentCount > max) {
            value = data[i][j];
            max = currentCount;
        }
    }
}

System.out.println(value + "=" + max);

Output

2=4

Basically iter over all values and count the appearances of each of those values. This approach (brute-force) is very inefficient.

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you @Daniel but this is my college assignment and i can only use an array and for loops to solve this problem.
@ArpitPatel Can you use a HashMap to store counts?
Sorry no, I can only use the Array.
@ArpitPatel If you can only use arrays and loops, I suggest you sort the data, and the count how many times each the values appear in consecutive order
Sorting might not be allowed, either
|
0

One possibility would be to use a hash map to keep track of the values along with the number of times each occurs:

private void commonElevation(int[][] data) {
    Map<Integer, Integer> counts = new HashMap<>();

    for (int i=0; i < data.length; i++) {
        for (int j=0; j < data[i].length; j++) {
            int count = counts.get(data[i][j]) == null ? 0 : counts.get(data[i][j]);
            counts.put(data[i][j], ++count);
        }
    }

    int frequent = Integer.MIN_VALUE;
    for (Integer value : counts.values()) {
        if (value > frequent) frequent = value;
    }

    System.out.println("most frequent value is: " + frequent);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.