How to find repeated rows of a matrix in C#

2 Answers

0 votes
using System;
using System.Collections.Generic;

class RepeatedRowsFinder
{
    static void Main()
    {
        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);
    }

    static void FindRepeatedRows(int[,] matrix) {
        HashSet<string> seenRows = new HashSet<string>();
        HashSet<string> repeatedRows = new HashSet<string>();

        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        for (int i = 0; i < rows; i++) {
            string rowString = RowToString(matrix, i, cols);
            if (!seenRows.Add(rowString))
            {
                repeatedRows.Add(rowString);
            }
        }

        if (repeatedRows.Count == 0) {
            Console.WriteLine("No repeated rows found.");
        }
        else {
            Console.WriteLine("Repeated rows:");
            foreach (var row in repeatedRows) {
                Console.WriteLine(row);
            }
        }
    }

    static string RowToString(int[,] matrix, int rowIndex, int cols) {
        List<string> rowElements = new List<string>();
        
        for (int j = 0; j < cols; j++) {
            rowElements.Add(matrix[rowIndex, j].ToString());
        }
        
        return string.Join(",", rowElements);
    }
}



/*
run:

Repeated rows:
1,2,3
4,5,6

*/

 



answered May 24, 2025 by avibootz
0 votes
using System;
using System.Collections.Generic;

class MatrixDuplicateFinder
{
    // Helper function to convert a row to a string for comparison
    private static string RowToString(List<int> row) {
        return string.Join(",", row);
    }

    // Function to find repeated rows in the matrix
    private static void FindRepeatedRows(List<List<int>> matrix) {
        Dictionary<string, int> rowCount = new Dictionary<string, int>();

        foreach (var row in matrix) {
            string pattern = RowToString(row);
            if (rowCount.ContainsKey(pattern)) {
                rowCount[pattern]++;
            }
            else {
                rowCount[pattern] = 1;
            }
        }

        Console.WriteLine("Repeated Rows:");
        foreach (var entry in rowCount) {
            if (entry.Value > 1) {
                Console.WriteLine($"Row: [{entry.Key}] - Repeated {entry.Value} times");
            }
        }
    }

    static void Main()
    {
        List<List<int>> matrix = new List<List<int>>
        {
            new List<int> { 1, 2, 3 },
            new List<int> { 4, 5, 6 },
            new List<int> { 1, 2, 3 },
            new List<int> { 7, 8, 9 },
            new List<int> { 4, 5, 6 },
            new List<int> { 0, 1, 2 },
            new List<int> { 4, 5, 6 }
        };

        FindRepeatedRows(matrix);
    }
}



/*
run:

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

*/

 



answered May 24, 2025 by avibootz
...