An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Unless I'm missing something, this is basically no different from count characters in a string. So
We need a container
public class NumberItem
{
public string Item { get; set; }
public int Occurrences { get; set; }
}
A method
public static List<NumberItem> GetAllNumbers(string[] values)
{
IOrderedEnumerable<NumberItem> itemsGroup = (
from chr in values
group chr by chr into grp
select new NumberItem
{
Item = grp.Key,
Occurrences = grp.Count()
})
.ToList()
.OrderBy(item => item.Item.ToString());
return (from item in itemsGroup select item).ToList();
}
Usage
var values = "81,0,88,82,7,18,12,82,7,88,0".Split(',');
var result = Operations.GetAllNumbers(values);
Get a specific value
var specificFind = result.FirstOrDefault(numberItem => numberItem.Item == "82");
if (specificFind != null)
{
MessageBox.Show($"{specificFind.Occurrences}");
}
Or we can convert all string items to int
public static class Extensions
{
public static int[] ToIntegerArray(this string[] sender)
{
var intArray = Array
.ConvertAll(sender,
(input) => new
{
IsInteger = int.TryParse(input, out var integerValue),
Value = integerValue
})
.Where(result => result.IsInteger)
.Select(result => result.Value)
.ToArray();
return intArray;
}
}
Change the container
public class NumberItem
{
public int Item { get; set; }
public int Occurrences { get; set; }
}
Call it
var values = "81,0,88,82,7,18,12,82,7,88,0".Split(',');
var result = Operations.GetAllNumbers(values.ToIntegerArray());
Check a specific number
var specificFind = result.FirstOrDefault(numberItem => numberItem.Item == 82);
if (specificFind != null)
{
MessageBox.Show($"{specificFind.Occurrences}");
}
For individual lines
Class
public class Item
{
public char Character { get; set; }
public int Occurrences { get; set; }
public int Code { get; set; }
public override string ToString() => $"{Character} - {Occurrences}";
}
In the following code, lines array represents lines from a file, didn't see a reason to do an actual read as mocked or read it's all the same. Once done iterate the Dictionary.
string[] lines = {
"8182,2114,9871,3335,5653,1812,5503,5310,9234,8864,0841,6947,0275,5765,0869,5877,0160,5120,0107,3023,8970,6360,4241",
"0251,1292,8111,7166,6149,5676,6301,5207,3242,2205,0765,9177,9988,4080,6972,5337,9173,4805,4720,8152,8805,5028,8625"
};
Dictionary<int, List<Item>> dictionary = new Dictionary<int, List<Item>>();
for (int index = 0; index < lines.Length; index++)
{
dictionary.Add(index, Operations.GetNumbersOnly(lines[index]));
}
In the Operations class
public static List<Item> GetNumbersOnly(string values)
=> GetAllItems(values).Where(item => item.Code.Between(48, 57)).ToList();
And
public static List<Item> GetAllItems(string values)
{
var itemsGroup = (
from chr in values.ToCharArray()
group chr by chr into grp
select new Item
{
Character = grp.Key,
Occurrences = grp.Count(),
Code = Convert.ToInt32((int)grp.Key)
})
.ToList()
.OrderBy(item => item.Character.ToString());
return (from item in itemsGroup select item).ToList();
}