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 check if a matrix rows contain numbers without repetition in Pascal

1 Answer

0 votes
program CheckMatrixRows;

const
  Rows = 3;
  Cols = 3;

type
  TMatrix = array[1..Rows, 1..Cols] of Integer;

function HasUniqueNumbersInRow(const Row: array of Integer): Boolean;
var
  Seen: array of Boolean;
  i, MaxValue: Integer;
begin
  // Find the actual maximum number in the row
  MaxValue := Row[0];
  for i := 1 to High(Row) do
    if Row[i] > MaxValue then
      MaxValue := Row[i];

  // Allocate memory safely based on the maximum value in the row
  SetLength(Seen, MaxValue + 1);
  
  // Initialize Seen array to False
  for i := 0 to High(Seen) do
    Seen[i] := False;

  for i := 0 to High(Row) do
  begin
    if Seen[Row[i]] then
    begin
      HasUniqueNumbersInRow := False;
      Exit;
    end;
    Seen[Row[i]] := True;
  end;
  HasUniqueNumbersInRow := True;
end;


function CheckMatrixRows(const Matrix: TMatrix): Boolean;
var
  i, j: Integer;
  Row: array of Integer;
begin
  for i := 1 to Rows do
  begin
    SetLength(Row, Cols);
    for j := 1 to Cols do
      Row[j - 1] := Matrix[i, j];
    if not HasUniqueNumbersInRow(Row) then
    begin
      CheckMatrixRows := False;
      Exit;
    end;
  end;
  CheckMatrixRows := True;
end;

var
  Matrix: TMatrix = (
    (1, 2, 3),
    (4, 5, 6),
    (7, 8, 7)
  );
begin
  if CheckMatrixRows(Matrix) then
    WriteLn('All rows contain unique numbers.')
  else
    WriteLn('Some rows contain repeated numbers.');
end.


  
(*
run:

Some rows contain repeated numbers.
  
*)  



 



answered May 23, 2025 by avibootz
...