On Sep 3, 2006, at 8:12 AM, Robert Woodhead wrote:
I need a to take a delimited list and remove all duplicate instances.
Simpler code does not always mean faster, it depends on the cost of
the individual operations, and in many cases, on the expected
structure of the input parameters. However, here is a simpler way
to do what you want (off the top of my head, untested, may contain
bugs, your mileage may vary)
Function removeDuplicates(sourceString as string,delim as string)
As string
// remove duplicate elements from sourceString
dim i as Integer
dim cString,finalString,token as String
finalString = ""
// add a delimiter to the end of the source string
cString = sourceString + ","
// find the first delimiter in the string
i = cString.indexOf(delim)
// while there are delimiters remaining in the string
while (i <> 0)
// extract the first token and delimiter
token = cString.left(i)
// add to the final string
finalString = finalString + token
// remove all the matching tokens+delimiters from the source
string
cString.replaceAll(token,"")
// find the next delimiter
i = cString.indexOf(delim)
wend
// result has an extra delimiter, so we must remove it
return finalString.left(finalString.len-1)
End Function
Alternatively --
Function removeDuplicates(sourceString as string,delim as string) As
string
dim sourceList() as String = Split(sourceString, delim)
dim d as new Dictionary
for i as Integer = 0 to UBound(sourceList)
d.Value(sourceList(i)) = true
next
dim lastKeyIndex as Integer = d.Count - 1
dim outputList(lastKeyIndex) as String
for i as Integer = 0 to lastKeyIndex
outputList(i) = d.Key(i)
next
return Join(outputList, delim)
End Function
Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>