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
*/