• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Deleting duplicates from an array

Lets say you have read 30 names from a file into an array with a format like  names: array[1..40] of string[30]; and there are a number of of duplicates in the file and therefor also the array-as you have read the names into the array- please could you write a procedrue which goes through the array and removes all duplicates from the array.

  • 3
  • 2
1 Solution
koopaAuthor Commented:
Adjusted points to 100
koopaAuthor Commented:
Please could you reply to the question as soon as possible as I am writing exams on this work in about two weeks
Here is a procedure that will remove duplicate strings from an array.  It will work with your example (array[1..40] of string[30]), or with any array of strings.  Just define it in the type declaration.


{written by scrapdog 9/25/98}

  t_string_array = array[1..40] of string[30];

procedure remove_duplicates(var s :t_string_array;
                            var size :integer);

  i,j  :integer;

  for i := 1 to size do
    for j := i+1 to size do
      if s[i] = s[j] then s[j] := '';      {this loop tests for duplicates}
  j := 1;
  for i := 1 to size do
    if s[i] <> '' then begin               {this loop removes them}
      s[j] := s[i];
      j := j + 1;
  size := j;


The procedure accepts as parameters the array and its size.  It will return the edited array and its new size.  For example, if, before you call, your array is of size 40 and contains 4 duplicates, the statements

size := 40;
remove_duplicates(x_array, size);

will remove the duplicates from the array, and 'size' will now equal 36, and only elements 1 to 36 of the array will be relevant.

Let me know if you don't understand something.



Additional note:  in the example I gave you, it might be hard to read because of the font that experts-exchange uses in its answers.

In the line that looks like this:

if s[i] = s[j] then s[j] := '';

it is important to point out that these are two APOSTROPHES with NO space in between them, *NOT* a quotation mark.  The same goes for the line that is 3 lines below it.  Hopefully you weren't confused by this.

koopaAuthor Commented:
Thank you for the brilliant answer it helped me alot !
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now