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

51,810 answers

573 users

How to get unique values from two arrays in Pascal

1 Answer

0 votes
program GetUniqueValuesProgram;

type
  TIntArray = array of Integer;

function getUniqueValues(arr1, arr2: TIntArray): TIntArray;
var
  i, j, k: Integer;
  found: Boolean;
begin
    SetLength(getUniqueValues, 0);
    for i := 0 to High(arr1) do
    begin
    found := False;
    for j := 0 to High(arr2) do
      if arr1[i] = arr2[j] then
        found := True;
    if not found then
    begin
      SetLength(getUniqueValues, Length(getUniqueValues) + 1);
      getUniqueValues[High(getUniqueValues)] := arr1[i];
    end;
  end;

  for j := 0 to High(arr2) do
  begin
    found := False;
    for i := 0 to High(arr1) do
      if arr2[j] = arr1[i] then
        found := True;
    if not found then
    begin
      SetLength(getUniqueValues, Length(getUniqueValues) + 1);
      getUniqueValues[High(getUniqueValues)] := arr2[j];
    end;
  end;

  for i := 0 to High(getUniqueValues) - 1 do
    for j := 0 to High(getUniqueValues) - i - 1 do
      if getUniqueValues[j] > getUniqueValues[j + 1] then
      begin
        k := getUniqueValues[j];
        getUniqueValues[j] := getUniqueValues[j + 1];
        getUniqueValues[j + 1] := k;
      end;
end;

var
  arr1, arr2, result: TIntArray;
  i: Integer;
begin
  arr1 := [1, 3, 6, 8, 12, 90];
  arr2 := [2, 3, 5, 6, 7, 8, 96];

  result := getUniqueValues(arr1, arr2);

  for i := 0 to High(result) do
    Write(result[i], ' ');
end.



(*
run:

1 2 5 7 12 90 96 

*)

 



answered Feb 16, 2025 by avibootz
...