10

Hi I'm using Delphi and I have a StringList with this items:

45
A15
015
A15
A15
45

I want to process it and make a second stringlist that will have the number of appearance of each element:

45 [2]
015 [1]
A15 [3]

How can I do this with Delphi?

1
  • I wish I had so cool homework when I was a kid. This is for fun. Jun 8, 2011 at 17:17

3 Answers 3

10

You could use a dictionary:

Frequencies := TDictionary <String, Integer>.Create;
try
  // Count frequencies
  for Str in StringList do
    begin
    if Frequencies.ContainsKey (Str) then
      Frequencies [Str] := Frequencies [Str] + 1
    else
      Frequencies.Add (Str, 1);
    end; 

   // Output results to console
   for Str in Frequencies.Keys do
     WriteLn (Str + ': ' + IntToStr (Frequencies [Str]));
finally
  FreeAndNil (Frequencies);
end;

The only problem might be that the order in which the results appear is completely random and dependes on the inner working of the hash map.

Thanks to daemon_x for the full unit code:

program Project1;

{$APPTYPE CONSOLE}

uses SysUtils, Classes, Generics.Collections;

var Str: String;
    StringList: TStrings;
    Frequencies: TDictionary <String, Integer>;

begin
  StringList := TStringList.Create;

  StringList.Add('45');
  StringList.Add('A15');
  StringList.Add('015');
  StringList.Add('A15');
  StringList.Add('A15');
  StringList.Add('45');

  Frequencies := TDictionary <String, Integer>.Create;

  try
  // Count frequencies
  for Str in StringList do
    begin
      if Frequencies.ContainsKey (Str) then
        Frequencies [Str] := Frequencies [Str] + 1
      else
        Frequencies.Add (Str, 1);
    end;

   // Output results to console
   for Str in Frequencies.Keys do
     WriteLn (Str + ': ' + IntToStr (Frequencies [Str]));

finally
  StringList.Free;
  FreeAndNil(Frequencies);
end;

end.
7
  • 1
    @downvoter: could you please explain your reasons? Downvoting without a reason does not help anyone and is not very fair either IMHO. Jun 8, 2011 at 10:44
  • @Smasher - I was the downvoter because there were some issues in TDictionary in D2009. But I hope that OP has update for it so I've edited your answer to take it back. Of course it's a good and fast solution but as I remember adding items to the TDictionary in D2009 took a long time without update.
    – user532231
    Jun 8, 2011 at 10:51
  • @smasher. Thank you for your help. I cant find the Dictionary component! How can I use this with Delphi 2009? Jun 8, 2011 at 11:23
  • 1
    Just add Generics.Collections to your uses list. Jun 8, 2011 at 11:24
  • @smasher code uses ..., Generics.Collections; type Frequencies = TDictionary<String, integer>; try // Count frequencies for Str IN FL do //Left side cannot be assigned to begin if Frequencies.ContainsKey(Str) then //expression expected Frequencies[Str]:= Frequencies[Str] + 1 else Frequencies.Add(Str, 1); end; // Output results to console for Str in Frequencies.Keys do WriteLn (Str + ': ' + IntToStr(Frequencies[Str])); finally FreeAndNil (Frequencies); end; code I get many errors Jun 8, 2011 at 11:49
3
  1. Sort the original list,

    list1.sort;
    
  2. create a new list

    list2:=TStringList.Create;
    
  3. iterate over the sorted list to count every different item and store the a count in the objects field of the resulting list (or if you don't use it already, just typecast the count into a pointer and store it as the object).

    previtem:=list1[0];
    count:=1;
    for i:=1 to list1.count-1 do
     begin
      if list1[i]=previtem then 
        inc(count)
      else
       begin
        list2.addObject(previtem,pointer(count));
        previtem:=list1[i];
        count:=1;
       end;
     end;
    list2.addObject(previtem,pointer(count));
    

finally, iterate again to add the count to the string

  for i:=0 to list2.count-1 do
    list2.items[i]:=list2[i]+' ['+inttostr(list2.objects[i])+']';
3

I coded this on my head as I don't have Delphi installed as of now. Let me know how it works for you. Stringlist1 is the original list with the items, stringlist2 is empty and will be used to store what you want.

for i := 0 to stringlist1.Count - 1 do
begin
    if (stringlist2.Values[stringlist1[i]] = '') then
        stringlist2.Values[stringlist1[i]] := '1'
    else
        stringlist2.Values[stringlist1[i]] :=
            IntToStr(StrToInt(stringlist2.Values[stringlist1[i]]) + 1);
end;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.