How to resolve the algorithm Remove duplicate elements step by step in the Modula-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Remove duplicate elements step by step in the Modula-2 programming language

Table of Contents

Problem Statement

Given an Array, derive a sequence of elements in which all duplicates are removed. There are basically three approaches seen here:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Remove duplicate elements step by step in the Modula-2 programming language

Source code in the modula-2 programming language

MODULE RemoveDuplicates;

FROM STextIO IMPORT
  WriteLn;
FROM SWholeIO IMPORT
  WriteInt;

TYPE
  TArrayRange = [1 .. 7];
  TArray = ARRAY TArrayRange OF INTEGER;

VAR
  DataArray, ResultArray: TArray;
  ResultIndex, LastResultIndex, Position: CARDINAL;
  IsNewNumber: BOOLEAN;

BEGIN
  (* Set the data. *);
  DataArray[1] := 1;
  DataArray[2] := 2;
  DataArray[3] := 2;
  DataArray[4] := 3;
  DataArray[5] := 4;
  DataArray[6] := 5;
  DataArray[7] := 5;

  ResultArray[1] := DataArray[1];
  LastResultIndex := 1;
  Position := 1;
  WHILE Position < HIGH(DataArray) DO
    INC(Position);
    IsNewNumber := TRUE;
    ResultIndex := 1;
    WHILE (ResultIndex <= LastResultIndex) AND IsNewNumber DO
      IF DataArray[Position] = ResultArray[ResultIndex] THEN
        IsNewNumber := FALSE;
      END;
      INC(ResultIndex);
    END;
    IF IsNewNumber THEN
      INC(LastResultIndex);
      ResultArray[LastResultIndex] := DataArray[Position];
    END
  END;
  FOR ResultIndex := 1 TO LastResultIndex DO
    WriteInt(ResultArray[ResultIndex], 1);
    WriteLn;
  END;
END RemoveDuplicates.


  

You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Maxima programming language
You may also check:How to resolve the algorithm Filter step by step in the JavaFX Script programming language
You may also check:How to resolve the algorithm Mertens function step by step in the J programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the Factor programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the ATS programming language