c# - Compare/count values in a System.Collections.ArrayList -


I am scrubbing 5 files for a specific value. I do not expect any other values, but because it is for my own educational purposes, I want to apply to calculate, compare and print the most popular values.

For example:

  ArrayLite ARNAM = new Arrayist (); ArrName.Add ("TOM") arrName.Add ("TOM") arrName.Add ("TOM") arrName.Add ("TOM")  

I think the result is going to be Tom, but being a newbie, I really do not know how to proceed.

Any ideas, suggestions or examples are highly appreciated. Thank you

You can use a dictionary (.NET 2.0+) so that each of the repeated number values :

Dictionary & lt; String, int & gt; Calculation = New Dictionary & lt; String, int & gt; (); Foreach (string name in arrName) {int count; If (count. Tregate value (name, count out)) {calculations [name] = count + 1; } And {count.Add (name, 1); }} // and then find the most popular value: string is the most popular; Int max = 0; Foreach (name string in the sense) {int count = count [name]; If (calculation> maximum) {most popular = name; Max = counting; }} // Print this console. Type ("Most Popular Value: {0}", Most Local);

If you are using C # 3.0 (.NET 3.5+) then use it:

  var mostPopular = (named by arrName. Cast & lt; string & gt; () The name of the group named G. G. Calculation (Select descending GK). FirstOver Default (); Console. ("Most Popular Value: {0}", Most Popular ?? "None");  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -