473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to determine the # of unique values in a simple arraylist...?

Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL

I want the routine to return a value of four (4) unique items.

thx in advance

Jun 1 '07 #1
6 3775
I have no idea how the "best" way to do this would be (i.e. most efficient)
but here's one way it certainly could be done...

Private Function CountTheUniqueStates(ByVal objListOfStates As ArrayList) As
Integer

Dim objUnique As New ArrayList

For Each strState As String In objListOfStates
If Not objUnique.Contains(strState) Then
objUnique.Add(strState)
End If
Next

Return objUnique.Count

End Function

"magellan" <ma******@discussions.microsoft.comwrote in message
news:92**********************************@microsof t.com...
Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL

I want the routine to return a value of four (4) unique items.

thx in advance

Jun 1 '07 #2
magellan wrote:
Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL

I want the routine to return a value of four (4) unique items.

thx in advance
The most efficient way (in terms of scalability) I believe would be to
use a dictionary. Loop throught the strings and if it doesn't exist in
the dictionary (ContainsKey), add it. When you are done, the Count
property contains the number of strings added.

You can even use the value in the dictionary to count how many there are
of each, if you would like that.

--
Göran Andersson
_____
http://www.guffa.com
Jun 1 '07 #3
Yes, using a dictionary would be my preference. However, not sure if using
an ArrayList is all that clever if it just contains strings (using VS
2003?). So:
Private Function CountTheUniqueStates(ByVal objListOfStates As List(Of
String)) As Integer

Dim objUnique As New Dictionary(Of String, String)

For Each strState As String In objListOfStates
If Not objUnique.ContainsKey(strState) Then
objUnique.Add(strState, strState)
End If
Next

Return objUnique.Keys.Count

End Function


"Göran Andersson" <gu***@guffa.comwrote in message
news:O5**************@TK2MSFTNGP05.phx.gbl...
magellan wrote:
>Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL

I want the routine to return a value of four (4) unique items.

thx in advance

The most efficient way (in terms of scalability) I believe would be to use
a dictionary. Loop throught the strings and if it doesn't exist in the
dictionary (ContainsKey), add it. When you are done, the Count property
contains the number of strings added.

You can even use the value in the dictionary to count how many there are
of each, if you would like that.

--
Göran Andersson
_____
http://www.guffa.com

Jun 1 '07 #4
Mucho Gracis' --Much Appreciated..:-)

"Robin Tucker" wrote:
Yes, using a dictionary would be my preference. However, not sure if using
an ArrayList is all that clever if it just contains strings (using VS
2003?). So:
Private Function CountTheUniqueStates(ByVal objListOfStates As List(Of
String)) As Integer

Dim objUnique As New Dictionary(Of String, String)

For Each strState As String In objListOfStates
If Not objUnique.ContainsKey(strState) Then
objUnique.Add(strState, strState)
End If
Next

Return objUnique.Keys.Count

End Function


"Göran Andersson" <gu***@guffa.comwrote in message
news:O5**************@TK2MSFTNGP05.phx.gbl...
magellan wrote:
Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL

I want the routine to return a value of four (4) unique items.

thx in advance
The most efficient way (in terms of scalability) I believe would be to use
a dictionary. Loop throught the strings and if it doesn't exist in the
dictionary (ContainsKey), add it. When you are done, the Count property
contains the number of strings added.

You can even use the value in the dictionary to count how many there are
of each, if you would like that.

--
Göran Andersson
_____
http://www.guffa.com


Jun 1 '07 #5
=?Utf-8?B?bWFnZWxsYW4=?= <ma******@discussions.microsoft.comwrote in
news:92**********************************@microsof t.com:
Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL

I use a hashtable (or dictionary). Add each entry as a key - and catch any
duplicate key entry exceptions. Or as others have said... you can check use
ContainsKey.

Jun 1 '07 #6
Spam Catcher wrote:
=?Utf-8?B?bWFnZWxsYW4=?= <ma******@discussions.microsoft.comwrote in
news:92**********************************@microsof t.com:
>Hi,

I'd appreciae any advice on how to do this in VB 2005.

I have a simple array;e..g, a list of States, e.g., CA, WA, ID, AL, and
etc...

I like to determine how many unqiue "States" are in this array...ie.,
duplicate entires, are igorned...

If this list has this sample:

CA
WA
ID
WA
CA
AL


I use a hashtable (or dictionary). Add each entry as a key - and catch any
duplicate key entry exceptions. Or as others have said... you can check use
ContainsKey.
I would strongly advice against using exceptions in that way, unless of
course you consider duplicate values to be an exceptional situation.

--
Göran Andersson
_____
http://www.guffa.com
Jun 2 '07 #7

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

Similar topics

1
by: jason | last post by:
NEWBIE - Hello. I've been able to do the following in other languages (perl,jscript,etc.) but am a asp.net newbie and need some help. I have this textbox that has a bunch of data in it. At the...
0
by: Bubbles | last post by:
Hello. New to ASP.NET and struggling on this one. I have a text file with a bunch of text in it. Throughout the file words followed by a ":" will appear. I need to pull every such string out...
1
by: Joep | last post by:
Hi, In VB.net I have an array which contains different values. For example: array(0) = 2 array(1) = 2 array(2) = 2 array(3) = 3 array(4) = 3 array(5) = 6 array(6) = 6
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
4
by: Jon Paal | last post by:
I am using a custom server control in my webpage which renders some html. while it creates the html, it develops an arraylist of values. Is there a way to capture the arraylist from the control...
9
by: Brian Tkatch | last post by:
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...
6
by: Pavel Maly | last post by:
Hi, how do I access values in an ArrayList which is a part of another ArrayList? I know I can define a class and then it is quite simple, but this is just an auxiliary application to compute some...
3
by: =?Utf-8?B?RWR3aW4=?= | last post by:
What I am trying to do is come up with a way to determine how many unique extensions there are within a directory and its sub directories. Within the loop that recurses root folder, I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.