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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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.
 
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
 
Mucho Gracis' --Much Appreciated..:-)

Robin Tucker said:
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
 
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.
 
Spam said:
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.
 
Back
Top