Max and Min Item

MiPakTeh 1,476 Reputation points
2022-02-25T13:47:21.177+00:00

Hi All,

Check for maxima and minima item every line is wrong.Can some tell me. If possible line 2 the maxima show 13(2) and minima show 6(2).

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.Threading.Tasks;
using System.Windows.Forms;

namespace TestFor_25
{
    public partial class Form1 : Form
    {

        List<string> Data1 = new List<string>();
        List<string> Data2 = new List<string>();



        public Form1()
        {

            InitializeComponent();
            foreach (var s in File.ReadLines(@"C:\Users\family\Documents\Tool_Ori_1.txt"))
            {
                listBox1.Items.Add(s);
                Data1.Add(s);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void button2_Click(object sender, EventArgs e)
        {
            int min = int.MaxValue;
            int max = int.MinValue;
            int Value = 0;


            foreach (var item in Data1)
            {
                listBox2.Items.Add(item);

                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_);

                    Data2.Add(NumberCount_.ToString());

                    Value = NumberCount_;
                    if (Value < min)
                    {
                        min = Value;
                    }
                    if (Value > max)
                    {
                        max = Value;
                    }
                }
                listBox2.Items.Add("Max Numbers =" + max + "     " + "Min Numbers =" + min);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            const string sPath = "C:\\Users\\family\\Documents\\save.txt";

            System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
            foreach (var item in listBox2.Items)
            {
                SaveFile.WriteLine(item.ToString());
            }
            SaveFile.Close();

            MessageBox.Show("Programs saved!");

        }
    }
}

text file : 

6576,8548,3444,0362,1854,2888,3598,5447,8334,4365,7459,9416,0460,5473,3938,0080,9953,8449,5176,7066,0217,9183,0385
8501,2994,9669,6236,5845,0334,4862,2989,9713,5632,7701,3169,8470,6549,0556,5522,5193,5977,9095,5777,8281,2343,7443
8380,9371,5663,5472,3418,3589,5491,0251,5943,6540,7597,9083,3356,4490,8101,3297,9197,5489,9811,5827,1995,8108,9009

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,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-02-28T08:00:26.557+00:00

    @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:

    178336-image.png


    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 112.9K Reputation points
    2022-02-26T08:35:01.55+00:00

    Maybe you must reorganise the code:

    private void button2_Click(object sender, EventArgs e)
    {
       foreach (var item in Data1)
       {
          int min = int.MaxValue;
          int max = int.MinValue;
          int Value = 0;
    . . .