by "Jochen" <jotel99@[EMAIL PROTECTED]
>
Nov 22, 2007 at 05:11 PM
Hi
Fister typed:
> Is there an elegant way to determine which string is mostly
> represented in a TStringList? For instance if I've got the following
> the function should return 'Delphi':
>
> StringList[0] := 'Pascal';
> StringList[1] := 'Pascal';
> StringList[2] := 'Delphi';
> StringList[3] := 'Delphi';
> StringList[4] := 'Delphi';
>
> I've made my own solution where I sort the TStringList and loop the
> list until the string changes and then I determine the count and
> compare it to a max value which is set if the count value if larger
> than max value.
Since sorting is not required u could also use a hashset. The problem as
always with hashsets is collision detection and solving that.
u could use list or tree but if u know the size of the stringlist u
could simply create a bigger hashset than stringlist.
maybe hashsize:=2*size+1 and use quite simple collision solving.
while not end of stringlist
pick next element from stringlist
calc hashvalue of the string
if hashsetelement[field] is empty -> store the string in there.
counter = 1
else if hashsetelement[field] = string then inc counter
else collisionhandling
( maybe simple solution like field := (field+1) mod hashsize
until empty field or string found ) and store it there or
increase counter.
now walk through the hashset and pick the one(s) with highest count.
Using a "good" function to create the keys this could be very fast way.
But when using poor function and easy solving technique it could become
a nightmare too ;)
Greetz
Jochen
--
http://radio789.net.ms
- Radio 789 - We play it ALL
Radiostream: http://stream789.net.ms