Counting Strings

Hey,
First of all I apologize for my English. It is not my first language.
I am new with Delphi.
I want to make a program that counts duplicated strings in a ListBox.
My program has a 2 component a button and a List box. If I push the button it must count how many strings or dabble and show it in a messageBox.
can somebody help me plz?

PS: I have tried with Array and For statement, but I didnt succeed.
kh420Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrewjbCommented:
If speed isn't a problem:

[Listbox called 'LB', Label called 'Label1']

procedure CountDoubles;
var
  lCount : integer;
  lIndex : integer;
  lPos   : integer;
begin
  lCount := 0;

  for lIndex := LB.Items.Count - 1 downto 1 do
  begin
    lPos := LB.Items.Find( LB.Items[lIndex] );
    if ( lPos >= 0 ) and ( lPos < lIndex ) then
    begin
      Inc( lCount );
    end;
  end;

  Label1.Caption = IntToStr(lCount) + ' duplicates';
end;

   
0
andrewjbCommented:
ooopss... 'Find' should be 'IndexOf'

and

after Label1.Caption

it should be

:=

instead of

=
0
mocartsCommented:
my version :)

procedure TForm1.Button1Click(Sender: TObject);
var
  st: TStringList;
  i, idx: integer;
  sMsg: string;
begin
  st := TStringList.Create;
  try
    for i := 0 to ListBox1.Items.Count -1 do begin
      idx := st.indexOf(ListBox1.Items[i]);
      if idx > -1 then
        st.Objects[idx] := pointer(integer(st.Objects[idx]) + 1)
      else
        st.AddObject(ListBox1.Items[i], pointer(0));
    end;
    for i := 0 to st.Count -1 do
      if integer(st.Objects[i]) > 0 then
      begin
        if sMsg <> '' then sMsg := sMsg + #13;
        sMsg := sMsg + '[' + inttostr(integer(st.Objects[i])+1) +'] ' + st[i];
      end;
    if sMsg <> '' then
      ShowMessage('Lines has duplicates:'+#13 + sMsg);
  finally
    st.Free;
  end;
end;

it's better of course to make this proc to some general by passing TStrings (ListBox.Items or Memo.Lines) to it and dealing with them.
wbr, mo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
my version :-))

unit checkfor_duplicates_u;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function checkfor_dub_strings(AStringList : TStrings) : Integer;
var
  sl : TStringList;
begin
  result := 0;  //pre-init
  sl := TStringList.Create;
  try
    sl.Sorted := true;
    sl.Duplicates := dupIgnore;
    sl.Assign(AStringList);
    result := AStringList.Count - sl.Count;
  finally
    sl.Free;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage('There are '+inttostr(checkfor_dub_strings(ListBox1.Items))+' Duplicate Entries');
end;

end.

meikl ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

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.