# Count in Loop give the result unstable at the middle line

1,476 Reputation points
2023-05-08T09:24:32.53+00:00

Hi All,

I have code to find Min and Max the result after count 0 to 9. I have problem with line 4 and lin5 .

5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760 =>4

0 = 11

1 = 7

2 = 6

3 = 6

4 = 10

5 = 7

6 = 14

7 = 14

8 = 10

9 = 7

Min = 6 , ItemMin = 2 || Min = 6 , ItemMin = 3 || Max = 14 , ItemMax = 6 => 4Max = 14 , ItemMax = 7 => 4

5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760 =>5

0 = 11

1 = 7

2 = 6

3 = 6

4 = 10

5 = 7

6 = 14

7 = 14

8 = 10

9 = 7

Min = 6 , ItemMin = 2 || Min = 6 , ItemMin = 3 || Max = 14 , ItemMax = 6 => 5Max = 14 , ItemMax = 7 => 5

then we cannot proceed with click button2.

bellow code we test;

``````using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bat_5
{
public partial class Form1 : Form
{
List<string> Data1 = new List<string>();
List<string> Data2 = new List<string>();

List<ItemInfo> Items = new List<ItemInfo>();

public static List<string> list = new List<string>();

int Lines = 0;
int Lines_a = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
.Skip(1)
.Select(s => s.Split(new[] { ',' }, 3)[2]))
{
listBox1.Items.Add(++Lines + "  " + s);
}

}

private void button1_Click(object sender, EventArgs e)
{
foreach (var item in Data1)
{
++Lines_a;

listBox2.Items.Add(item + "   =>" + Lines_a);

var Items = new List<ItemInfo>();

int min = int.MaxValue;
int max = int.MinValue;

for (int j = 0; j <= 9; j++)
{
String Number_ = j.ToString();
int NumberCount_ = item.ToString().Replace(",", "").ToCharArray().Count(c => c.ToString() == Number_);
listBox2.Items.Add(Number_ + " = " + NumberCount_);
Items.Add(new ItemInfo() { Index = Convert.ToInt32(Number_.ToString()), Value = NumberCount_ });
}

foreach (ItemInfo ITM in Items)
{
if (ITM.Value < min)
{
min = ITM.Value;
}
if (ITM.Value > max)
{
max = ITM.Value;
}
}

string List = "";
string List_ = "";
string List_1 = "";

foreach (ItemInfo ITM in Items)
{

if (ITM.Value == min)
{
List += "Min = " + min.ToString() + " , " + "ItemMin = " + ITM.Index.ToString() + "  " + " || ";
}
if (ITM.Value == max)
{
List_ += "Max = " + max.ToString() + " , " + "ItemMax = " + ITM.Index.ToString() + "     => ";
}

}
listBox2.Items.Add(List + "   " + List_);
Data2.Add(List + "   " + List_ + "  " + List_1);
}
}

private void button2_Click(object sender, EventArgs e)
{
var duplicates = Data2.Select((t, i) => new { Index = i + 1, Text = t }).GroupBy(g => g.Text).Where(g => g.Count() > 1).ToList();
foreach (var item in duplicates)
{
var result = item.Select(i => i.Index + "  " + i.Text).ToArray();
string text = string.Join("       ", result);

}
}

private void button3_Click(object sender, EventArgs e)
{

}

}
}
public partial class ItemInfo
{
public int Index;
public int Value;

public override string ToString()
{
return Index.ToString() + " => " + Value.ToString();
}
}

``````
C#
C#
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.
10,691 questions

1. 24,496 Reputation points Microsoft Vendor
2023-05-12T05:28:59.7166667+00:00

@MiPakTeh, based on my test, I could get your wanted result. I guess that maybe you changed the code somewhere.

Here is a small completed code example you could refer to.

``````  public partial class Form1 : Form
{
List<string> Data1 = new List<string>();
List<string> Data2 = new List<string>();

List<ItemInfo> Items = new List<ItemInfo>();

public static List<string> list = new List<string>();

int Lines = 0;
int Lines_a = 0;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
foreach (var item in Data1)
{
++Lines_a;

listBox2.Items.Add(item + "   =>" + Lines_a);

var Items = new List<ItemInfo>();

int min = int.MaxValue;
int max = int.MinValue;

for (int j = 0; j <= 9; j++)
{
String Number_ = j.ToString();
int NumberCount_ = item.ToString().Replace(",", "").ToCharArray().Count(c => c.ToString() == Number_);
listBox2.Items.Add(Number_ + " = " + NumberCount_);
Items.Add(new ItemInfo() { Index = Convert.ToInt32(Number_.ToString()), Value = NumberCount_ });
}

foreach (ItemInfo ITM in Items)
{
if (ITM.Value < min)
{
min = ITM.Value;
}
if (ITM.Value > max)
{
max = ITM.Value;
}
}

string List = "";
string List_ = "";
string List_1 = "";

foreach (ItemInfo ITM in Items)
{

if (ITM.Value == min)
{
List += "Min = " + min.ToString() + " , " + "ItemMin = " + ITM.Index.ToString() + "  " + " || ";
}
if (ITM.Value == max)
{
if(!List_.Contains("ItemMax"))
{
List_ += "Max = " + max.ToString() + " , " + "ItemMax = " + ITM.Index.ToString() + "     => ";
}

}

}
listBox2.Items.Add(List + "   " + List_+" "+Lines_a);
Data2.Add(List + "   " + List_ + "  " + List_1);
}
}

private void button2_Click(object sender, EventArgs e)
{
var duplicates = Data2.Select((t, i) => new { Index = i + 1, Text = t }).GroupBy(g => g.Text).Where(g => g.Count() >= 1).ToList();
foreach (var item in duplicates)
{
var result = item.Select(i => i.Index + "  " + i.Text).ToArray();
string text = string.Join("       ", result);

}
}

private void Form1_Load(object sender, EventArgs e)
{
.Select(s => s.Split(new[] { ',' }, 3)[2]))
{
listBox1.Items.Add(++Lines + "  " + s);
}
}
}
public partial class ItemInfo
{
public int Index;
public int Value;

public override string ToString()
{
return Index.ToString() + " => " + Value.ToString();
}
}
``````

Tested result:

Best Regards,

Jack