Re: Unique an array of strings

Tech-Archive recommends: Fix windows errors by optimizing your registry



Mythran wrote:
"Brian Tkatch" <Maxwell_Smart@xxxxxxxxxxxxxxx> wrote in message
news:1154531218.502140.128060@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm looking for a simple way to unique an array of strings. I came up
with this. Does it make sense? Am i missing anything? (Testing seems to
show it to work.)

Public Function Unique(ByVal List() As String) As String()

' Returns the unique values of in array, in an array.

Dim Temp As New
System.Collections.Specialized.StringCollection()

For Each Current_String As String In List
If Not Temp.Contains(Current_String) Then
Temp.Add(Current_String)
Next

ReDim Unique(Temp.Count - 1)

Temp.CopyTo(Unique, 0)

End Function

B.


Public Function Unique(ByVal List As String()) As String()
Dim temp As StringCollection = New StringCollection()

For Each current As String in List
If Not temp.Contains(current)
temp.Add(current)
End If
Next

Return temp
End Function

You don't need to ReDim Unique ... you could just use the "Return" statement
to return the array.
Shortened it up a wittle...and afaik, this would be how you remove duplicate
array entries...

HTH,
Mythran

Thanx for the feedback.

Hmm.. aren't you returning a StringCollection, when the Function
declared an array of String? Is there a way to do that? I put it back
into an array to return the same type.

As for the Redim, i thought it would be nicer to explicitly set the
size. But mostly, VB didn't like me using the function name without
doing something with it first (the green line, and at run time i got an
error about not instantiating it yet.)

B.

.



Relevant Pages

  • Re: Help Please - Searching a large text file
    ... an array with about 500 elements to start with, and only Redim it when the ... Dim LensDataArrayAs String, FileNumber As Long ...
    (comp.lang.basic.visual.misc)
  • Re: Help Please - Searching a large text file
    ... Actually you can speed that part of it up about three or four times by writing it in such a way that it does not Redim the array every single time through the loop. ... Dim LensDataArrayAs String, FileNumber As Long ...
    (comp.lang.basic.visual.misc)
  • Re: ReDim an array AFTER its loaded into another array
    ... '.RteInfo2 as string 'etc depending on how many pieces of data you need ... Redim vRteFactsAy ... ' RFA is the route facts array ... Once a "master" array is formed, it looks like the component arrays ...
    (microsoft.public.excel.programming)
  • Re: Error on UBound with Dynamic Array
    ... determine the difference in length of the string. ... Doug Steele, Microsoft Access MVP ... ReDim Preserve arrSplit+ 1) ... need to use a dynamic array and may be checking the boundries, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Error on UBound with Dynamic Array
    ... Hadn't realized ReDim was 'expensive'. ... determine the difference in length of the string. ... need to use a dynamic array and may be checking the boundries, ... If locEnd = 0 Then ...
    (microsoft.public.access.modulesdaovba)