Counting duplicates before remove (StringList)

Hi.

I have some strings that i pass to a string list from a For loop.
I don't want the duplicates so i set the list to dupIgngore but ...

I want to count the duplicates before i remove them and pass the string to the string list like that :
John (25)
Peter (12)

I'm trying to find the best approach cause speed matters here (more than 5000 lines to edit).

Thanks in advance.
LVL 16
CodedKAsked:
Who is Participating?
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
here's a couple of methods
You'll notice that Button2 processes a lot faster :-)
I'm cheating by using the stringlist's "object" property to hold the count

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Memo3: TMemo;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    SourceList, UniqueList, DupesList: TStringList;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  SourceList := TStringList.Create;
  UniqueList := TStringList.Create;
  DupesList := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SourceList.Free;
  UniqueList.Free;
  UniqueList.Clear;
  DupesList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  starttime, endtime: tdatetime;
  i, idx: integer;
  oneword: string;

begin
  sourcelist.assign(memo1.lines);
  UniqueList.Clear;
  DupesList.Clear;
  starttime := now;
  for i := 0 to pred(sourcelist.count) do
  begin
    oneword := SourceList[i];
    if UniqueList.IndexOf(oneword) <> -1 then
      DupesList.Values[oneword] := IntToStr(StrToIntDef(DupesList.Values[oneword], 0) + 1)
    else
      UniqueList.Add(oneword);
  end;
  endtime := now;
  memo2.Lines.Assign(UniqueList);
  memo3.Lines.Assign(DupesList);
  label1.caption := FormatDateTime('nn:ss.zzz', EndTime-StartTime);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  starttime, endtime: tdatetime;
  i, idx: integer;
  oneword: string;

begin
  sourcelist.assign(memo1.lines);
  UniqueList.Clear;
  starttime := now;
  for i := 0 to pred(sourcelist.count) do
  begin
    oneword := SourceList[i];
    idx := UniqueList.IndexOf(oneword);
    if idx = -1 then
      UniqueList.AddObject(oneword, tobject(0))
    else
      UniqueList.Objects[idx] := tObject( integer(UniqueList.Objects[idx]) + 1 );
  end;
  endtime := now;
  memo2.Lines.Assign(UniqueList);
  for i := 0 to pred(UniqueList.Count) do
    memo3.Lines.Add(UniqueList[i]+ ' = ' + IntToStr(Integer(UniqueList.Objects[i])));
  label1.caption := FormatDateTime('nn:ss.zzz', EndTime-StartTime);
end;


end.

0
 
CodedKAuthor Commented:
Sweeet :)
Thank you Loki.
0
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.

All Courses

From novice to tech pro — start learning today.