Question is:
Write a program that reads data into an array of type int. Valid inputs are from 0 to 10. Your program should determine how many values were inputted. Output a list of distinct entries and a count of how many times that entry occurred.
I have so far:
using System;
using System.Collections;
namespace ConsoleApplication25
{
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
string inValue;
Console.WriteLine("Please enter a value from 0-10");
Console.WriteLine("To end the program, type 11");
for (int i = 0; i < 11; i++)
{
Console.Write("Enter Value:", i);
inValue = Console.ReadLine();
i = int.Parse(inValue);
list.Add(i);
list.Remove(11);
list.Sort();
}
int[] c = list.ToArray(typeof(int)) as int[];
foreach (int value in c)
{
Console.WriteLine(value);
}
Console.WriteLine("There are {0} values.", list.Count);
}
}
}
Where I’m stuck at is displaying a count of each value. I have tried setting up an if with a counter, and setting up cases with breaks and have been unsuccessful. We haven’t started using LINQ yet. Any suggestions or hints would be greatly appreciated.
Thanks,
Jason
Use
Dictionary<int, int>to store value and count of how many times it occured. Short example: