How to find repeated rows of a matrix in VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

Class RepeatedRowsFinder
	Public Shared Sub Main()
        Dim matrix As Integer(,) = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 3},
        {7, 8, 9},
        {4, 5, 6},
        {0, 1, 2},
        {4, 5, 6}}

        FindRepeatedRows(matrix)
    End Sub

    Private Shared Sub FindRepeatedRows(ByVal matrix As Integer(,))
        Dim seenRows As HashSet(Of String) = New HashSet(Of String)()
        Dim repeatedRows As HashSet(Of String) = New HashSet(Of String)()
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)

        For i As Integer = 0 To rows - 1
            Dim rowString As String = RowToString(matrix, i, cols)

            If Not seenRows.Add(rowString) Then
                repeatedRows.Add(rowString)
            End If
        Next

        If repeatedRows.Count = 0 Then
            Console.WriteLine("No repeated rows found.")
        Else
            Console.WriteLine("Repeated rows:")

            For Each row In repeatedRows
                Console.WriteLine(row)
            Next
        End If
    End Sub

    Private Shared Function RowToString(ByVal matrix As Integer(,), ByVal rowIndex As Integer, ByVal cols As Integer) As String
        Dim rowElements As List(Of String) = New List(Of String)()

        For j As Integer = 0 To cols - 1
            rowElements.Add(matrix(rowIndex, j).ToString())
        Next

        Return String.Join(",", rowElements)
    End Function
End Class

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

 



answered May 24, 2025 by avibootz
edited May 24, 2025 by avibootz
0 votes
Imports System
Imports System.Collections.Generic

Class MatrixDuplicateFinder
    Private Shared Function RowToString(ByVal row As List(Of Integer)) As String
        Return String.Join(",", row)
    End Function

    Private Shared Sub FindRepeatedRows(ByVal matrix As List(Of List(Of Integer)))
        Dim rowCount As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)()

        For Each row In matrix
            Dim pattern As String = RowToString(row)

            If rowCount.ContainsKey(pattern) Then
                rowCount(pattern) += 1
            Else
                rowCount(pattern) = 1
            End If
        Next

        Console.WriteLine("Repeated Rows:")

        For Each entry In rowCount

            If entry.Value > 1 Then
                Console.WriteLine($"Row: [{entry.Key}] - Repeated {entry.Value} times")
            End If
        Next
    End Sub

	Public Shared Sub Main()
        Dim matrix As List(Of List(Of Integer)) = New List(Of List(Of Integer)) From {
            New List(Of Integer) From {
                1, 2, 3
            },
            New List(Of Integer) From {
                4, 5, 6
            },
            New List(Of Integer) From {
                1, 2, 3
            },
            New List(Of Integer) From {
                7, 8, 9
            },
            New List(Of Integer) From {
                4, 5, 6
            },
            New List(Of Integer) From {
                0,1, 2
            },
            New List(Of Integer) From {
                4, 5, 6
            }
        }

        FindRepeatedRows(matrix)

    End Sub
End Class


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

 



answered May 24, 2025 by avibootz
...