I need to be able to count the frequency of items in a hashtablewith
multiple occurance of differing terms. Can anyone help me please, I'm
stuck!
Would greatly appreciate any commenst/suggestions/code-samples.
I'm not sure I understand the question. What items in the hashtable are
you trying to count? The keys are unique...are you saying that you may
have a given value stored in the hashtable multiple times, under multiple
keys.
What is it exactly you're trying to count?
As for the general question of how to implement a histogram, ironically a
common .NET implementation would be to use a Dictionary<TKey, TValue>
(which is a kind of hashtable), using the items you're counting as keys
and the current count as the value for the key.
If you're trying to count the occurences of specific values in your
hashtable, then this would probably work for you:
Hashtable hash = /* initialize as input */;
Dictionary<object, int> dictHistogram = new dictHistogram<object,
int>();
foreach (object in hash.Values)
{
int cvalue;
if (!dictHistogram.TryGetValue(object, out cvalue))
{
cvalue = 0;
}
dictHistogram[object] = cvalue + 1;
}
// at this point, you can enumerate the KeyValuePair<TKey, TValue>
// instances in the dictionary to retrieve the objects with their
// counts.
This assumes, of course, that the objects in your original hashtable have
overridden Equals() and GetHashCode(), so that they can be useful as keys
in the dictionary (or that you are simply looking for instance identity,
so that the base implementation of those methods suffices).
If you're trying to do something else, you should probably rephrase the
question so that you're more specific about what you want to do.
Pete