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,907 questions

51,839 answers

573 users

How to find repeated rows of a matrix in Pascal

1 Answer

0 votes
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
  
*)  



 



answered May 24, 2025 by avibootz
...