Solved

Deleting duplicates from an array

Posted on 1998-09-25
5
280 Views
Last Modified: 2010-04-16
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.
Thanks

 
0
Comment
Question by:koopa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:koopa
ID: 1215685
Adjusted points to 100
0
 

Author Comment

by:koopa
ID: 1215686
Please could you reply to the question as soon as possible as I am writing exams on this work in about two weeks
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 100 total points
ID: 1215687
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}


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

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

var
  i,j  :integer;

begin
  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;
    end;
  size := j;
end;

---------------------------------

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.


~scrappy

 

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215688
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.

0
 

Author Comment

by:koopa
ID: 1215689
Thank you for the brilliant answer it helped me alot !
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
websites monitor c++ or delphi 9 453
How do you invoke TCharHelper 1 2,050
How to slide a FMX tabcontrol tabitem up from the bottom 3 1,181
Secure Boot on W8 & 8.1 8 48
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question