>  -----Original Message-----
>  Behalf Of Scott Goelzer
>  Subject: Re: Remove duplicate code improvements
>  
>  Such a clever use of dictionaries, but
>  
>  I messed around with this code and found that Join function 
>  will not play nice since d.keys returns a variant array, not 
>  a string array.
>  
>  I tried d.keys().stringtype, but RB refuses.
>  
>  Not too familiar with dictionaries, is there any way to use 
>  the d.keys functions without running another loop?
>  
>  On Sep 3, 2006, at 1:20 PM, Joe Huber wrote:
>  
>  > Seems to me this could be simplified even further
>  >
>  > Function removeDuplicates(sourceString as string,delim as 
>  string) As 
>  > string
>  >   dim sourceList() as String = Split(sourceString, delim)
>  >   dim d as new Dictionary
>  >   for each item as string in sourceList
>  >     d.Value(item) = true
>  >   next
>  >
>  >   return Join(d.keys, delim)
>  > End Function
>  >

Here is an alternative that should work.

Function removeDuplicates(sourceString as string,delim as string) As string
  dim sourceList() as String = Split(sourceString, delim)
  dim d as new Dictionary
  dim sourceListCount As integer = UBound(sourcelist)
  for i as integer = sourceListCount downto 0
    if not d.HasKey(sourceList(i)) then 
      d.Value(sourceList(i)) = true
    else
      sourcelist.Remove(i)
    end
  next
  return Join(sourceList, delim)
End Function

If your original string is sorted alphabetically (or if you don't mind
sorting the array after the split, which would mean you would be returning a
string with items in a different order than the original), then there is a
(probably) faster way to do this, without using a dictionary. 

Function removeDuplicates(sourceString as string,delim as string) As string
  dim sourceList() as String = Split(sourceString, delim)
  sourceList.Sort 'needed only if sourceString wasn't already sorted
  dim sourceListCount As integer = UBound(sourcelist)
  for i as integer = sourcelistcount downto 1
    if sourceList(i-1) = sourceList(i) then
      sourceList.Remove(i)
    end
  next
  return Join(sourceList, delim)
End Function

HTH,
Walter


_______________________________________________
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>

Reply via email to