473,758 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unique an array of strings

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.Collecti ons.Specialized .StringCollecti on()

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

ReDim Unique(Temp.Cou nt - 1)

Temp.CopyTo(Uni que, 0)

End Function

B.

Aug 2 '06 #1
9 5682

"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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.Collecti ons.Specialized .StringCollecti on()

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

ReDim Unique(Temp.Cou nt - 1)

Temp.CopyTo(Uni que, 0)

End Function

B.
Public Function Unique(ByVal List As String()) As String()
Dim temp As StringCollectio n = New StringCollectio n()

For Each current As String in List
If Not temp.Contains(c urrent)
temp.Add(curren t)
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
Aug 2 '06 #2
Mythran wrote:
"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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.Collecti ons.Specialized .StringCollecti on()

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

ReDim Unique(Temp.Cou nt - 1)

Temp.CopyTo(Uni que, 0)

End Function

B.

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

For Each current As String in List
If Not temp.Contains(c urrent)
temp.Add(curren t)
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 StringCollectio n, 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.

Aug 2 '06 #3

"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
Mythran wrote:
>"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******* *************** @h48g2000cwc.go oglegroups.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.Collecti ons.Specialized .StringCollecti on()

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

ReDim Unique(Temp.Cou nt - 1)

Temp.CopyTo(Uni que, 0)

End Function

B.

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

For Each current As String in List
If Not temp.Contains(c urrent)
temp.Add(curren t)
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 StringCollectio n, 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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(String)), String())
End Function
:D That's what I get for not testing it heh..

HTH,
Mythran

Aug 2 '06 #4
Mythran wrote:
"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
Mythran wrote:
"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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.Collecti ons.Specialized .StringCollecti on()

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

ReDim Unique(Temp.Cou nt - 1)

Temp.CopyTo(Uni que, 0)

End Function

B.


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

For Each current As String in List
If Not temp.Contains(c urrent)
temp.Add(curren t)
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 StringCollectio n, 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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(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.

Aug 2 '06 #5
>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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(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
Aug 2 '06 #6

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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(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_Collec tions_ArrayList _ToArray_1_a699 8a77.htm>

ArrayList.ToArr ay 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.

Aug 4 '06 #7

"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(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_Collec tions_ArrayList _ToArray_1_a699 8a77.htm>

ArrayList.ToArr ay 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>.ToAr ray( ... ) .
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(Ge tType(String))
Console.WriteLi ne(array) ' Returns String[]

Mythran
Aug 4 '06 #8

Mythran wrote:
"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(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_Collec tions_ArrayList _ToArray_1_a699 8a77.htm>

ArrayList.ToArr ay 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>.ToAr ray( ... ) .
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(Ge tType(String))
Console.WriteLi ne(array) ' Returns String[]

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

Return DirectCast(temp .ToArray(GetTyp e(String)), String())

is the DirectCast extra or not?

BTW, thanx for all the detailed responses.

B.

Aug 4 '06 #9

"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
>
Mythran wrote:
>"Brian Tkatch" <Ma***********@ ThePentagon.com wrote in message
news:11******* *************** @m79g2000cwm.go oglegroups.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(c urrent)
temp.Add(curren t)
End If
Next

Return DirectCast(temp .ToArray(GetTyp e(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_Collec tions_ArrayList _ToArray_1_a699 8a77.htm>

ArrayList.ToArr ay 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>.ToAr ray( ... )
.
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(Ge tType(String))
Console.WriteL ine(array) ' Returns String[]

Mythran

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

Return DirectCast(temp .ToArray(GetTyp e(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
Aug 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13655
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
6
2972
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I want to use the array with an ID Something like,
2
4616
by: m|sf|t | last post by:
All, I have a snippet of code below. In my foreach, I would like to process only 1 item per hdd value, in the case below, the echo $tib . "\n"; would only display 1 and 2, not 1,1,1,1,2,2,2,2. It doesn't matter which one shows, because I only want the unique value for hdd. $tibs = array( "tib1" => array("drive" => "C", "progress" => "on", "hdd" => "1", "partition" => "1", "compression" => "3", "backup" => "K:\\"), "tib2" =>...
2
2773
by: Raterus | last post by:
Hi, I'm looking for ideas for the most efficient way to accomplish this. I have a string representing names a person goes by. "John Myers Joe John Myers" And I need to parse it in such a way that I end up with an array of UNIQUE strings that appear in the original string (In no particular order) arr(0) = "John" arr(1) = "Myers"
4
3828
Ispep
by: Ispep | last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without saying I'm not asking you to solve the question - that won't help come exam time :(). Anyway I have a CSV delimited file in the following format; STRING,INT STRING,INT,INT,INT,INT,INT STRING,INT,INT,INT,INT,INT STRING,INT,INT,INT,INT,INT ...
10
2091
by: Krustov | last post by:
$rambo="daffy duck"; $rambo="mad max"; $rambo="daffy duck"; $rambo="superman"; etc etc How do i remove duplicate strings from a array ? . ('daffy duck' could appear more than twice in the array)
35
2486
by: RobG | last post by:
Seems developers of mobile applications are pretty much devoted to UA sniffing: <URL: http://wurfl.sourceforge.net/vodafonerant/index.htm > -- Rob
2
2334
by: dniom | last post by:
Hi. Can you help me please? Is there any way to show a unique random item from an array, every time you refresh the page? In other words how do I show the items from an array in random order, one by one, by reloading the page? Moreover the page have to be refreshed by pressing the "submit" button... Thats what I achieved so far :) but it doesn't display the UNIQUE array item every time the page is reloaded... as you may guess :) <html>...
1
1572
by: charindal | last post by:
i would like some help on how to retrieve array indexes. i have the following scenario: List <string> alphabet = new ....; alphabet.add("a"); alphabet.add("b"); alphabet.add("a"); alphabet.add("c"); alphabet.add("a");
0
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8744
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7287
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.