C# tutorials
8. Frequency count of array values i

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.

To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag a Button to Form 1.

6. Drag a Rich Textbox to Form 1. Resize the textbox if you like.

7. Double-click on the button. The file Form1.cs will open with the caret ready to write code that will run when button 1 is clicked.

8. Write or paste this code where the caret is:

string[] arr = { "count", "me", "and", "me", "and", "me", "as", "well" };
Dictionary<string, int> dic = new Dictionary<string, int>();
dic = mkCount(arr, dic);
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, int> pair in dic)
  sb.AppendLine((string.Format("{0} {1}", pair.Key, pair.Value)));
richTextBox1.Text = sb.ToString();

9. Write or paste this code right above the line private void button1_Click(object sender, EventArgs e):

public Dictionary<string, int> mkCount(string[] arr, Dictionary<string, int> dic)
  foreach (var item in arr)
    if (!dic.ContainsKey(item))
      dic.Add(item, 1);
  return dic;

10. Press F5 to start debugging the program.

11. Click on the button. This text should appear in the textbox:

count 1
me 3
and 2
as 1
well 1

Comments on the first code snippet

Line 1: The array arr is created.

Line 2: The dictionary dic is created. A dictionary is a collection of keys and values. In this case, the keys are of the type string and the values of the type int. A string is a sequence of zero or more Unicode characters. An int is an integer in the range -2,147,483,648 to 2,147,483,647.

Line 3: The frequency of each value in the array arr is counted with the user-defined method mkCount.

Line 4: The stringbuilder sb is created. A stringbuilder is immutable meaning that the number of characters in the string it encapsulates can be expanded.

Line 5-8: The contents of the dictionary dic are added to the stringbuilder sb. {0} represents the keys and {1} the values. The space between {0} and {1} represents a literal space. You can change the space if you like, for example to \t if you want a tab between each key and value pair instead.

Line 9: The final result is written to the textbox. The stringbuilder sb must be converted to a string with the method ToString().

Comments on the second code snippet

Line 1-15: Code for the user-defined method mkCount. A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method. Line 3 in the first code snippet above, dic = mkCount(arr, dic);, provides an example of how this can be done.

Line 1: Dictionary<string, int> indicates what the method returns: a dictionary with keys of the type string and values of the type int. mkCount is the name of the method. mk is simply my initials; you can change the name of the method if you like as long as you don't use a word that is reserved by C#. If you really want to use a reserved word as the name of a method, you can if you precede the reserved word with the sign @, for example @string. string[] arr and Dictionary<string, int> dic are parameters required by the method.

Line 5-8: If the key isn't already in the dictionary, add the key and set the value to 1.

Line 9-12: If the key already is in the dictionary, increase the value with 1.

Line 14: Get the result.

Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.