Home All Groups Group Topic Archive Search About

Unique an array of strings

Author
2 Aug 2006 3:06 PM
Brian Tkatch
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.

Author
2 Aug 2006 3:31 PM
Mythran
Show quote Hide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1154531218.502140.128060@h48g2000cwc.googlegroups.com...
> 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
Author
2 Aug 2006 3:51 PM
Brian Tkatch
Mythran wrote:
Show quoteHide quote
> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
> news:1154531218.502140.128060@h48g2000cwc.googlegroups.com...
> > 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.
Author
2 Aug 2006 4:05 PM
Mythran
Show quote Hide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1154533868.444370.66540@h48g2000cwc.googlegroups.com...
> Mythran wrote:
>> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
>> news:1154531218.502140.128060@h48g2000cwc.googlegroups.com...
>> > 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.
>

woops, in this case I would do the following :D

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

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

    Return DirectCast(temp.ToArray(GetType(String)), String())
End Function


:D  That's what I get for not testing it heh..

HTH,
Mythran
Author
2 Aug 2006 5:57 PM
Brian Tkatch
Mythran wrote:
Show quoteHide quote
> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
> news:1154533868.444370.66540@h48g2000cwc.googlegroups.com...
> > Mythran wrote:
> >> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
> >> news:1154531218.502140.128060@h48g2000cwc.googlegroups.com...
> >> > 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.
> >
>
> woops, in this case I would do the following :D
>
> Public Function Unique(ByVal List As String()) As String()
>     Dim temp As ArrayList = New ArrayList()
>
>     For Each current As String in List
>         If Not temp.Contains(current)
>             temp.Add(current)
>         End If
>     Next
>
>     Return DirectCast(temp.ToArray(GetType(String)), String())
> End Function
>
>
> :D  That's what I get for not testing it heh..
>
> HTH,
> Mythran

Kewl. I didn't realize i could use an ArrayList here. A bit nicer.

With about an array(1000000) with about 400,000 entries (just my quikie
test) ArrayList was about 1/100 of a second faster. (Tested without
DirectCast.)

What's the DirectCast for?  The help also does it (well, it uses
CType()) but ToArray() itself is casting it. Why cast it again?

B.
Author
2 Aug 2006 6:53 PM
Mythran
Show quote Hide quote
>> woops, in this case I would do the following :D
>>
>> Public Function Unique(ByVal List As String()) As String()
>>     Dim temp As ArrayList = New ArrayList()
>>
>>     For Each current As String in List
>>         If Not temp.Contains(current)
>>             temp.Add(current)
>>         End If
>>     Next
>>
>>     Return DirectCast(temp.ToArray(GetType(String)), String())
>> End Function
>>
>>
>> :D  That's what I get for not testing it heh..
>>
>> HTH,
>> Mythran
>
> Kewl. I didn't realize i could use an ArrayList here. A bit nicer.
>
> With about an array(1000000) with about 400,000 entries (just my quikie
> test) ArrayList was about 1/100 of a second faster. (Tested without
> DirectCast.)
>
> What's the DirectCast for?  The help also does it (well, it uses
> CType()) but ToArray() itself is casting it. Why cast it again?
>
> B.
>

ToArray places all items in the ArrayList into an array and returns the
array as an object array.  I am using DirectCast to cast from an object
array to a string array :)  Sure, if you don't have Option Strict turned on,
it would perform the casting automatically, but I have a habit of turning on
Option Strict for my projects :)

HTH,
Mythran
Author
4 Aug 2006 12:48 PM
Brian Tkatch
Mythran wrote:
Show quoteHide quote
> >> woops, in this case I would do the following :D
> >>
> >> Public Function Unique(ByVal List As String()) As String()
> >>     Dim temp As ArrayList = New ArrayList()
> >>
> >>     For Each current As String in List
> >>         If Not temp.Contains(current)
> >>             temp.Add(current)
> >>         End If
> >>     Next
> >>
> >>     Return DirectCast(temp.ToArray(GetType(String)), String())
> >> End Function
> >>
> >>
> >> :D  That's what I get for not testing it heh..
> >>
> >> HTH,
> >> Mythran
> >
> > Kewl. I didn't realize i could use an ArrayList here. A bit nicer.
> >
> > With about an array(1000000) with about 400,000 entries (just my quikie
> > test) ArrayList was about 1/100 of a second faster. (Tested without
> > DirectCast.)
> >
> > What's the DirectCast for?  The help also does it (well, it uses
> > CType()) but ToArray() itself is casting it. Why cast it again?
> >
> > B.
> >
>
> ToArray places all items in the ArrayList into an array and returns the
> array as an object array.  I am using DirectCast to cast from an object
> array to a string array :)  Sure, if you don't have Option Strict turned on,
> it would perform the casting automatically, but I have a habit of turning on
> Option Strict for my projects :)
>
> HTH,
> Mythran

But isn't that only if you do not specify the Tyope in ToArray()?

According to the help: <URL:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_Collections_ArrayList_ToArray_1_a6998a77.htm>

ArrayList.ToArray Method (Type)
....
Copies the elements of the ArrayList to a new array of the specified
element type.
....
Parameters
type
The element Type of the destination array to create and copy elements
to

Return Value
An array of the specified element type containing copies of the
elements of the ArrayList.

What am i missing?

B.
Author
4 Aug 2006 3:17 PM
Mythran
Show quote Hide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1154695691.221359.159860@m79g2000cwm.googlegroups.com...
>
> Mythran wrote:
>> >> woops, in this case I would do the following :D
>> >>
>> >> Public Function Unique(ByVal List As String()) As String()
>> >>     Dim temp As ArrayList = New ArrayList()
>> >>
>> >>     For Each current As String in List
>> >>         If Not temp.Contains(current)
>> >>             temp.Add(current)
>> >>         End If
>> >>     Next
>> >>
>> >>     Return DirectCast(temp.ToArray(GetType(String)), String())
>> >> End Function
>> >>
>> >>
>> >> :D  That's what I get for not testing it heh..
>> >>
>> >> HTH,
>> >> Mythran
>> >
>> > Kewl. I didn't realize i could use an ArrayList here. A bit nicer.
>> >
>> > With about an array(1000000) with about 400,000 entries (just my quikie
>> > test) ArrayList was about 1/100 of a second faster. (Tested without
>> > DirectCast.)
>> >
>> > What's the DirectCast for?  The help also does it (well, it uses
>> > CType()) but ToArray() itself is casting it. Why cast it again?
>> >
>> > B.
>> >
>>
>> ToArray places all items in the ArrayList into an array and returns the
>> array as an object array.  I am using DirectCast to cast from an object
>> array to a string array :)  Sure, if you don't have Option Strict turned
>> on,
>> it would perform the casting automatically, but I have a habit of turning
>> on
>> Option Strict for my projects :)
>>
>> HTH,
>> Mythran
>
> But isn't that only if you do not specify the Tyope in ToArray()?
>
> According to the help: <URL:
> ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_Collections_ArrayList_ToArray_1_a6998a77.htm>
>
> ArrayList.ToArray Method (Type)
> ...
> Copies the elements of the ArrayList to a new array of the specified
> element type.
> ...
> Parameters
> type
> The element Type of the destination array to create and copy elements
> to
>
> Return Value
> An array of the specified element type containing copies of the
> elements of the ArrayList.
>
> What am i missing?
>
> B.
>

The ToArray method does not return an object() as I originally mentioned.
The type returned is System.Array.  When you have Option Strict turned on,
you can not set a string-array to <instance of ArrayList>.ToArray( ... ) .
The compiler will give you errors (I did, in fact, test this part out).

Otherwise, yes you are correct that it does create a string array (or array
of the specified type passed into ToArray).

Dim array As Array = list.ToArray(GetType(String))
Console.WriteLine(array)   ' Returns String[]

Mythran
Author
4 Aug 2006 4:48 PM
Brian Tkatch
Mythran wrote:
Show quoteHide quote
> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
> news:1154695691.221359.159860@m79g2000cwm.googlegroups.com...
> >
> > Mythran wrote:
> >> >> woops, in this case I would do the following :D
> >> >>
> >> >> Public Function Unique(ByVal List As String()) As String()
> >> >>     Dim temp As ArrayList = New ArrayList()
> >> >>
> >> >>     For Each current As String in List
> >> >>         If Not temp.Contains(current)
> >> >>             temp.Add(current)
> >> >>         End If
> >> >>     Next
> >> >>
> >> >>     Return DirectCast(temp.ToArray(GetType(String)), String())
> >> >> End Function
> >> >>
> >> >>
> >> >> :D  That's what I get for not testing it heh..
> >> >>
> >> >> HTH,
> >> >> Mythran
> >> >
> >> > Kewl. I didn't realize i could use an ArrayList here. A bit nicer.
> >> >
> >> > With about an array(1000000) with about 400,000 entries (just my quikie
> >> > test) ArrayList was about 1/100 of a second faster. (Tested without
> >> > DirectCast.)
> >> >
> >> > What's the DirectCast for?  The help also does it (well, it uses
> >> > CType()) but ToArray() itself is casting it. Why cast it again?
> >> >
> >> > B.
> >> >
> >>
> >> ToArray places all items in the ArrayList into an array and returns the
> >> array as an object array.  I am using DirectCast to cast from an object
> >> array to a string array :)  Sure, if you don't have Option Strict turned
> >> on,
> >> it would perform the casting automatically, but I have a habit of turning
> >> on
> >> Option Strict for my projects :)
> >>
> >> HTH,
> >> Mythran
> >
> > But isn't that only if you do not specify the Tyope in ToArray()?
> >
> > According to the help: <URL:
> > ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_Collections_ArrayList_ToArray_1_a6998a77.htm>
> >
> > ArrayList.ToArray Method (Type)
> > ...
> > Copies the elements of the ArrayList to a new array of the specified
> > element type.
> > ...
> > Parameters
> > type
> > The element Type of the destination array to create and copy elements
> > to
> >
> > Return Value
> > An array of the specified element type containing copies of the
> > elements of the ArrayList.
> >
> > What am i missing?
> >
> > B.
> >
>
> The ToArray method does not return an object() as I originally mentioned.
> The type returned is System.Array.  When you have Option Strict turned on,
> you can not set a string-array to <instance of ArrayList>.ToArray( ... ) .
> The compiler will give you errors (I did, in fact, test this part out).
>
> Otherwise, yes you are correct that it does create a string array (or array
> of the specified type passed into ToArray).
>
> Dim array As Array = list.ToArray(GetType(String))
> Console.WriteLine(array)   ' Returns String[]
>
> Mythran

So, then, to be clear, in the example you provided:

Return DirectCast(temp.ToArray(GetType(String)), String())

is the DirectCast extra or not?

BTW, thanx for all the detailed responses.

B.
Author
4 Aug 2006 5:48 PM
Mythran
Show quote Hide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1154710116.768954.57630@p79g2000cwp.googlegroups.com...
>
> Mythran wrote:
>> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
>> news:1154695691.221359.159860@m79g2000cwm.googlegroups.com...
>> >
>> > Mythran wrote:
>> >> >> woops, in this case I would do the following :D
>> >> >>
>> >> >> Public Function Unique(ByVal List As String()) As String()
>> >> >>     Dim temp As ArrayList = New ArrayList()
>> >> >>
>> >> >>     For Each current As String in List
>> >> >>         If Not temp.Contains(current)
>> >> >>             temp.Add(current)
>> >> >>         End If
>> >> >>     Next
>> >> >>
>> >> >>     Return DirectCast(temp.ToArray(GetType(String)), String())
>> >> >> End Function
>> >> >>
>> >> >>
>> >> >> :D  That's what I get for not testing it heh..
>> >> >>
>> >> >> HTH,
>> >> >> Mythran
>> >> >
>> >> > Kewl. I didn't realize i could use an ArrayList here. A bit nicer.
>> >> >
>> >> > With about an array(1000000) with about 400,000 entries (just my
>> >> > quikie
>> >> > test) ArrayList was about 1/100 of a second faster. (Tested without
>> >> > DirectCast.)
>> >> >
>> >> > What's the DirectCast for?  The help also does it (well, it uses
>> >> > CType()) but ToArray() itself is casting it. Why cast it again?
>> >> >
>> >> > B.
>> >> >
>> >>
>> >> ToArray places all items in the ArrayList into an array and returns
>> >> the
>> >> array as an object array.  I am using DirectCast to cast from an
>> >> object
>> >> array to a string array :)  Sure, if you don't have Option Strict
>> >> turned
>> >> on,
>> >> it would perform the casting automatically, but I have a habit of
>> >> turning
>> >> on
>> >> Option Strict for my projects :)
>> >>
>> >> HTH,
>> >> Mythran
>> >
>> > But isn't that only if you do not specify the Tyope in ToArray()?
>> >
>> > According to the help: <URL:
>> > ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_Collections_ArrayList_ToArray_1_a6998a77.htm>
>> >
>> > ArrayList.ToArray Method (Type)
>> > ...
>> > Copies the elements of the ArrayList to a new array of the specified
>> > element type.
>> > ...
>> > Parameters
>> > type
>> > The element Type of the destination array to create and copy elements
>> > to
>> >
>> > Return Value
>> > An array of the specified element type containing copies of the
>> > elements of the ArrayList.
>> >
>> > What am i missing?
>> >
>> > B.
>> >
>>
>> The ToArray method does not return an object() as I originally mentioned.
>> The type returned is System.Array.  When you have Option Strict turned
>> on,
>> you can not set a string-array to <instance of ArrayList>.ToArray( ... )
>> .
>> The compiler will give you errors (I did, in fact, test this part out).
>>
>> Otherwise, yes you are correct that it does create a string array (or
>> array
>> of the specified type passed into ToArray).
>>
>> Dim array As Array = list.ToArray(GetType(String))
>> Console.WriteLine(array)   ' Returns String[]
>>
>> Mythran
>
> So, then, to be clear, in the example you provided:
>
> Return DirectCast(temp.ToArray(GetType(String)), String())
>
> is the DirectCast extra or not?
>
> BTW, thanx for all the detailed responses.
>
> B.
>

It depends on whether or not Option Strict is turned on.  If it's turned on,
then it is requird (either DirectCast or CType).  If it's not turned on,
then it is not required.

HTH,
Mythran