Search code examples
c#mode

How can I get the mode (Statistic) in Visual Studio?


Basically, in a textbox (txtEl) I'm writing the length of the vector, then add it random numbers (from 1 to 500), showing all of them in a listbox. But I don't know how to get the mode from all those numbers.

        Random Aleatorio = new Random();
        int x = Convert.ToInt16(txtEl.Text);

        int[] elementos = new int [x];

        int moda = 0;

        for (int i = 0; i < elementos.Length; i++)
        {
            elementos[i] = Aleatorio.Next(1, 500);
            listEl.Items.Add(elementos[i].ToString());



        }

Solution

  • You can do something like this Im not real sure what listEl is but you should be able to use it here. If not just add them to a basic list also.

    var mode = (from item in listEl.Items
                             group item by item into g
                             orderby g.Count() descending
                             select g.Key).First();