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 columns contain numbers without repetition in Pascal

1 Answer

0 votes
program ColumnsUniqueNumbers;

const
  Rows = 3;
  Cols = 3;
 
type
  TMatrix = array[1..Rows, 1..Cols] of Integer;
  
function ColumnsHaveUniqueNumbers(const matrix: TMatrix; rows, cols: Integer): Boolean;
var
  j, i, k: Integer;
  columnSet: array of Integer;
  found: Boolean;
begin
  if (rows = 0) or (cols = 0) then
    Exit(True);

  for j := 1 to cols do  // 1-based indexing
  begin
    SetLength(columnSet, 0); // Reset columnSet for each column

    for i := 1 to rows do  // 1-based indexing
    begin
      found := False;
      for k := 0 to High(columnSet) do
      begin
        if columnSet[k] = matrix[i, j] then
        begin
          found := True;
          Break;
        end;
      end;
      
      if found then
        Exit(False);

      SetLength(columnSet, Length(columnSet) + 1);
      columnSet[High(columnSet)] := matrix[i, j];
    end;
  end;
  Exit(True);
end;

var
  matrix1: TMatrix = (
    (1, 4, 7),
    (2, 5, 8),
    (3, 6, 9)
  );  
  matrix2: TMatrix = (
    (1, 4, 7),
    (2, 4, 8),
    (3, 6, 9)
  );  
begin
  WriteLn('Matrix 1 columns have unique numbers: ', ColumnsHaveUniqueNumbers(matrix1, Rows, Cols)); 
  WriteLn('Matrix 2 columns have unique numbers: ', ColumnsHaveUniqueNumbers(matrix2, Rows, Cols)); 
end.

  
    
(*
run:
  
Matrix 1 columns have unique numbers: TRUE
Matrix 2 columns have unique numbers: FALSE
    
*)

 



answered May 28, 2025 by avibootz
...