Counting the frequency of items in a hashtable -- please help

A

almurph

Hi,


I need to be able to count the frequency of items in a hashtable with
multiple occurance of differing terms. Can anyone help me please, I'm
stuck!
Would greatly appreciate any commenst/suggestions/code-samples.

Thank you,
Al.
The confused!
 
A

almurph

   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

Hi Pete,

Sorry, I should have clarified - I have an arraylist with words in
it, like:

HT.Add("apple")
HT.Add("apple")
HT.Add("apple")
HT.Add("apple")
HT.Add("orange")
HT.Add("orange")

In this case: "apple" count would be 4 and "orange" count would be 2.

Sorry, does this change anything?
Apologies,
Al.
 
J

Jeff Johnson

Sorry, I should have clarified - I have an arraylist with words in
it, like:

In this case: "apple" count would be 4 and "orange" count would be 2.

An ArrayList is not a HashTable. Exactly HOW is HT declared?
 
A

almurph

An ArrayList is not a HashTable. Exactly HOW is HT declared?

sorry Jeff, I made a mistake, a typo. i am trying to count the items
in an arraylist *not* a hashtable.

Apologies,Al.
 
P

Peter Morris

If you can use LINQ and List<T> instead of ArrayList you could do something
like this

var al = new List<string>();
al.Add("Apples");
al.Add("Apples");
al.Add("Oranges");

var frequencies =
from v in al
group v by v into groupedData
select new { Name = groupedData.Key, Count = groupedData.Count() };

foreach (var info in frequencies)
Console.WriteLine("{0} = {1}", info.Name, info.Count);


or if it must be object and not string

var al = new List<object>();
al.Add("Apples");
al.Add("Apples");
al.Add("Oranges");

var frequencies =
from v in al
group v by v into groupedData
select new { Name = groupedData.Key.ToString(), Count =
groupedData.Count() };

foreach (var info in frequencies)
Console.WriteLine("{0} = {1}", info.Name, info.Count);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top