I'm newbie. I want count frequency when I input numbers by use dictionary. Because I use array[100]. Size of array: 3 Input: 1 2 3 Result: 1: 1 time 2: 1 time 3: 1 time 0: 97 times
Please help me ! Run result
Implement code:
{
Console.WriteLine("Count frequencies of array: ");
int[] arr = new int[100];
int n, i;
Console.Write("Input the number of elements to be stored in the array :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
//Create a dictionary to store the counts.
Dictionary<int, int> valCount = new Dictionary<int, int>();
foreach (int x in arr)
{
if (valCount.ContainsKey(x))
valCount[x]++;
else
valCount[x] = 1;
}
foreach (KeyValuePair<int, int> kvp in valCount)
Console.WriteLine("{0}: " + "{1} times", kvp.Key, kvp.Value);
Console.Write("Press any key to close the console app...");
Console.ReadKey();
}