How to find the element that appears once in an array where other elements appear in pairs with Pascal

1 Answer

0 votes
program FindUniqueElement;

const
  N = 9;

var
  arr: array[1..N] of integer;
  elemen: integer;
  i: integer;

begin
  // Initialize the array 
  arr[1] := 7;
  arr[2] := 2;
  arr[3] := 2;
  arr[4] := 4;
  arr[5] := 5;
  arr[6] := 3;
  arr[7] := 4;
  arr[8] := 5;
  arr[9] := 7;

  // Start with the first element 
  elemen := arr[1];

  // XOR all elements — duplicates cancel out, leaving the unique one 
  for i := 2 to N do
    elemen := elemen xor arr[i];

  writeln('The element that appears only once is: ', elemen);
end.



(*
run:

The element that appears only once is: 3

*)

 



answered Sep 5, 2025 by avibootz
...