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 repeated rows of a matrix in Java

2 Answers

0 votes
import java.util.Set;
import java.util.Arrays;
import java.util.HashSet;

public class RepeatedRowsFinder {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {1, 2, 3},
            {7, 8, 9},
            {4, 5, 6},
            {0, 1, 2},
            {4, 5, 6}
        };

        findRepeatedRows(matrix);
    }

    public static void findRepeatedRows(int[][] matrix) {
        Set<String> seenRows = new HashSet<>();
        Set<String> repeatedRows = new HashSet<>();

        for (int[] row : matrix) {
            String rowString = Arrays.toString(row);
            if (!seenRows.add(rowString)) {
                repeatedRows.add(rowString);
            }
        }

        if (repeatedRows.isEmpty()) {
            System.out.println("No repeated rows found.");
        } else {
            System.out.println("Repeated rows:");
            for (String row : repeatedRows) {
                System.out.println(row);
            }
        }
    }
}

   
   
/*
run:
   
Repeated rows:
[1, 2, 3]
[4, 5, 6]
   
*/

 



answered May 23, 2025 by avibootz
0 votes
import java.util.Map;
import java.util.List;
import java.util.Arrays;
import java.util.HashMap;

public class MatrixDuplicateFinder {
    // Helper function to convert a row to a string for comparison
    private static String rowToString(List<Integer> row) {
        return String.join(",", row.stream().map(String::valueOf).toArray(String[]::new));
    }

    // Function to find repeated rows in the matrix
    private static void findRepeatedRows(List<List<Integer>> matrix) {
        Map<String, Integer> rowCount = new HashMap<>();

        for (List<Integer> row : matrix) {
            String pattern = rowToString(row);
            rowCount.put(pattern, rowCount.getOrDefault(pattern, 0) + 1);
        }

        System.out.println("Repeated Rows:");
        for (Map.Entry<String, Integer> entry : rowCount.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println("Row: [" + entry.getKey() + "] - Repeated " + entry.getValue() + " times");
            }
        }
    }

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

        findRepeatedRows(matrix);
    }
}

   
   
/*
run:
   
Repeated Rows:
Row: [4,5,6] - Repeated 3 times
Row: [1,2,3] - Repeated 2 times
   
*/

 



answered May 23, 2025 by avibootz
...