@MiPakTeh , Based on your code, you want to calculate the number with the least number of occurrences and the number of times this number occurs.
I recommend that you use linq to solve the problem.
Code:
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in Data1)
{
listBox2.Items.Add(item);
List<int>list=new List<int>(){};
for (int j = 0; j <= 9; j++)
{
String Number_ = j.ToString();
int NumberCount_ = item.Replace(",", "").ToCharArray().Count(c => c.ToString() == Number_);
listBox2.Items.Add(j.ToString() + "-" + NumberCount_);
list.Add(NumberCount_);
Data2.Add(NumberCount_.ToString());
}
var result = list.GroupBy(i => i).Select(g => new { Number = g.Key, count = g.Count() });
var min = result.Where((x) => x.Number == result.Min(y => y.Number)).FirstOrDefault();
var max= result.Where((x) => x.Number == result.Max(y => y.Number)).FirstOrDefault();
string resultstr = string.Format("Max Numbers ={0}({1}) Min Numbers ={2}({3})", max.Number, max.count, min.Number, min.count);
listBox2.Items.Add(resultstr);
}
}
I added a int list and use GroupBy method to get the information you wanted.
Result:
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.