program MatrixDuplicateFinder;
uses
SysUtils; // IntToStr
const
Rows = 7;
Cols = 3;
type
TMatrix = array[1..Rows, 1..Cols] of Integer;
TStringArray = array of String;
TRowCountMap = array of Integer;
TBoolArray = array of Boolean; // To track printed rows
function RowToString(const Row: array of Integer): String;
var
i: Integer;
begin
RowToString := '';
for i := Low(Row) to High(Row) do
begin
RowToString := RowToString + IntToStr(Row[i]);
if i < High(Row) then
RowToString := RowToString + ',';
end;
end;
procedure FindRepeatedRows(Matrix: TMatrix);
var
RowStrings: TStringArray;
RowCount: TRowCountMap;
Printed: TBoolArray; // New array to track printed rows
i, j: Integer;
CurrentRow: array[0..Cols - 1] of Integer;
begin
SetLength(RowStrings, Rows);
SetLength(RowCount, Rows);
SetLength(Printed, Rows);
for i := 0 to High(Printed) do
Printed[i] := False; // Initialize all as not printed
for i := 1 to Rows do
begin
for j := 1 to Cols do
CurrentRow[j - 1] := Matrix[i, j];
RowStrings[i - 1] := RowToString(CurrentRow);
RowCount[i - 1] := 1;
end;
// Compare row patterns
for i := 0 to High(RowStrings) do
for j := i + 1 to High(RowStrings) do
if RowStrings[i] = RowStrings[j] then
begin
RowCount[i] := RowCount[i] + 1;
if i <> j then
RowCount[j] := RowCount[j] + 1;
end;
WriteLn('Repeated Rows:');
for i := 0 to High(RowStrings) do
if (RowCount[i] > 1) and not Printed[i] then
begin
WriteLn('Row [', RowStrings[i], '] - Repeated ', RowCount[i], ' times');
// Mark all occurrences of this row as printed
for j := 0 to High(RowStrings) do
if RowStrings[j] = RowStrings[i] then
Printed[j] := True;
end;
end;
var
Matrix: TMatrix = (
(1, 2, 3),
(4, 5, 6),
(1, 2, 3),
(7, 8, 9),
(4, 5, 6),
(0, 1, 2),
(4, 5, 6)
);
begin
FindRepeatedRows(Matrix);
end.
(*
run:
Repeated Rows:
Row [1,2,3] - Repeated 2 times
Row [4,5,6] - Repeated 3 times
*)