Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,788 questions

51,694 answers

573 users

How to find a common element in all rows of a given matrix with sorted rows in Java

3 Answers

0 votes
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class CommonElementMatrix {

    public static int findCommonElementInMatrixRows(List<List<Integer>> matrix) {
        int rows = matrix.size();
        if (rows == 0) return -1; // Handle empty matrix
        int cols = matrix.get(0).size();

        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < rows; i++) {
            map.put(matrix.get(i).get(0), map.getOrDefault(matrix.get(i).get(0), 0) + 1);
            for (int j = 1; j < cols; j++) {
                if (!matrix.get(i).get(j).equals(matrix.get(i).get(j - 1))) {
                    int val = matrix.get(i).get(j);
                    map.put(val, map.getOrDefault(val, 0) + 1);
                }
            }
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == rows) {
                return entry.getKey();
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        List<List<Integer>> matrix = Arrays.asList(
            Arrays.asList(1, 2, 3, 5, 36),
            Arrays.asList(4, 5, 7, 9, 10),
            Arrays.asList(5, 6, 8, 9, 18),
            Arrays.asList(1, 3, 5, 8, 24)
        );

        int result = findCommonElementInMatrixRows(matrix);
        if (result != -1) {
            System.out.println("Common element in all rows: " + result);
        } else {
            System.out.println("No common element found in all rows.");
        }
    }
}


/*
run:

Common element in all rows: 5

*/

 



answered Oct 3, 2025 by avibootz
0 votes
public class CommonElementMatrix {

    static final int MAX_VAL = 1000; // Assumes values are in range [0, 999]

    public static int findCommonElementInMatrixRows(int[][] matrix) {
        int[] map = new int[MAX_VAL];
        
        int rows = matrix.length;
        int cols = matrix[0].length;

        for (int i = 0; i < rows; i++) {
            map[matrix[i][0]]++;
            for (int j = 1; j < cols; j++) {
                if (matrix[i][j] != matrix[i][j - 1]) {
                    map[matrix[i][j]]++;
                }
            }
        }

        for (int i = 0; i < MAX_VAL; i++) {
            if (map[i] == rows) {
                return i;
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3, 5, 36},
            {4, 5, 7, 9, 10},
            {5, 6, 8, 9, 18},
            {1, 3, 5, 8, 24}
        };

        int result = findCommonElementInMatrixRows(matrix);
        if (result != -1) {
            System.out.println("Common element in all rows: " + result);
        } else {
            System.out.println("No common element found in all rows.");
        }
    }
}



/*
run:

Common element in all rows: 5

*/

 



answered Oct 3, 2025 by avibootz
0 votes
import java.util.HashMap;
import java.util.Map;

public class CommonElementMatrix {

    public static int findCommonElementInMatrixRows(int[][] matrix) {
        Map<Integer, Integer> map = new HashMap<>();

        int rows = matrix.length;
        int cols = matrix[0].length;

        for (int i = 0; i < rows; i++) {
            map.put(matrix[i][0], map.getOrDefault(matrix[i][0], 0) + 1);
            for (int j = 1; j < cols; j++) {
                if (matrix[i][j] != matrix[i][j - 1]) {
                    int val = matrix[i][j];
                    map.put(val, map.getOrDefault(val, 0) + 1);
                }
            }
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == rows) {
                return entry.getKey();
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3, 5, 36},
            {4, 5, 7, 9, 10},
            {5, 6, 8, 9, 18},
            {1, 3, 5, 8, 24}
        };

        int result = findCommonElementInMatrixRows(matrix);
        if (result != -1) {
            System.out.println("Common element in all rows: " + result);
        } else {
            System.out.println("No common element found in all rows.");
        }
    }
}



/*
run:

Common element in all rows: 5

*/

 



answered Oct 3, 2025 by avibootz
...