Re: what is the best datatype for..
- From: Chris Dunaway <dunawayc@xxxxxxxxx>
- Date: Mon, 19 Nov 2007 08:14:22 -0800 (PST)
On Nov 19, 9:53 am, calvert4r...@xxxxxxxxx wrote:
I need to some sort of data type that will hold a listing of ID's and
their counts/frequency. As I do some processing I get an ID back
which I need to store and keep an accurate count for how many times
that ID is listed. For example I might get a listing of these
numbers:
1,4,6,23,6,2,54,1,6,23,2,4,1,6
I will get those numbers one at a time and need to build something
like
1,3
4,2
6,4
23,2
2,2
43,1
In above number X,Y:
X- is the number
Y- is the count of the number
I then need to sort the data on the Y (the count) column:
6,4
1,3
4,2
23,2
2,2
43,1
Can anyone suggest how best to do this. Any suggestions or help is
much appreciated!
Thanks,
Calvert
As for data types, a general rule of thumb would be that if you plan
to do some calculations with it (like addition, subtraction, etc.)
then it should be some sort of numeric type (int, double, decimal).
If you are only going to store it, then use a string. For example,
even though a phone number is all digits, you should use a string
since you don't do calculations with a phone number.
For you problem, I think your best bet would be to use a generic
dictionary. Use the ID as the key and the value as the count. Read
each value in the array, check to see if it already exists in the
dictionary. If it does, then just increment the count, otherwise, add
it to the dictionary.
Dim values() As Integer = {1,4,6,23,6,2,54,1,6,23,2,4,1,6}
Dim idCount As New Dictionary(Of String, Integer)()
For Each i As Integer In values 'Check
each value in the array
If idCount.ContainsKey(i.ToString()) Then 'If the value
already exists in the dictionary
idCount(i.ToString) += 1
'increment the count for that value
Else
'otherwise
idCount.Add(i.ToString(), 1) 'add
the value to the dictionary
End If
Next
Hope this helps
Chris
.
- Follow-Ups:
- Re: what is the best datatype for..
- From: Wingot
- Re: what is the best datatype for..
- From: calvert4rent
- Re: what is the best datatype for..
- References:
- what is the best datatype for..
- From: calvert4rent
- what is the best datatype for..
- Prev by Date: Re: what is the best datatype for..
- Next by Date: Re: getting container element type with reflection
- Previous by thread: Re: what is the best datatype for..
- Next by thread: Re: what is the best datatype for..
- Index(es):
Relevant Pages
|