I need to indicate how many times the highest value of an arrangement is repeated

0

I must tell the console to print the number of times a value is repeated, in this case, it is the largest int, which would be the highest value of the vector, try a few things, but I am stuck, I will leave the code here .

If I can make you tell me which is the highest number, but I need you to tell me how many times the greater number or the int value is repeated.

I had one that was for when certain numbers were repeated, and I tried to edit it, but it did not work for me.

        int[] numero = new int[10];
        int i;
        int mayor = -1;
        int indice = 0;


        Console.Clear();
        Console.WriteLine("Ejercicio 1");
        Console.WriteLine("");
        Console.WriteLine("Leer 10 numeros enteros, almacenarlos en un vector y determinar el mayor");
        Console.WriteLine("");

        for (i = 0; i < numero.Length; i++)
        {
            Console.WriteLine("Digite un numero {0}", i);
            int.TryParse(Console.ReadLine(), out numero[i]);

        }

        for (i = 0; i < numero.Length; i++)
        {
            Console.WriteLine(i + " - " + numero[i]);


        }

        for (i = 0; i < numero.Length; i++)
        {
            if (numero[i] > mayor)
            {
                mayor = numero[i];
                indice = i;
            }

        }

        Console.WriteLine("El mayor es {0} y su posicion es {1}", mayor, indice);



        Console.ReadKey();
    
asked by Reymon Omar Garcia Alcantara 06.12.2018 в 02:09
source

2 answers

3

Since you already have the largest number in the vector, now we just need to go through the vector again and compare each of the positions of the vector ( numero[i] ) with the value of mayor and when the values are equal you increase to one the variable that you have as a counter.

I'll give you an example:

int contador = 0;

for (i = 0; i < numero.Length; i++)
{
    if (numero[i] == mayor)
    {
        contador++;
    }
}

Console.WriteLine("El número mayor del vector es {0} y se repite {1} veces", mayor, contador);
    
answered by 06.12.2018 / 14:40
source
1

The simplest way to do it is if you help linq, I'll give you an example

List<int> numero = new List<int>;

Console.Clear();
Console.WriteLine("Ejercicio 1");
Console.WriteLine("");
Console.WriteLine("Leer 10 numeros enteros, almacenarlos en un vector y determinar el mayor");
Console.WriteLine("");

for (int i = 0; i < 10; i++)
{
    Console.WriteLine("Digite un numero {0}", i);

    int temp=0;
    if(int.TryParse(Console.ReadLine(), out temp))
    {
        numero.Add(temp);
    }
}

int index =0;
foreach (int item in numero)
{
    Console.WriteLine("{0} - {1}", index, item);
    index++;
}

var result = (from item in numero
              group item by item into g
              orderby g.Count() descending
              select new {
                 item = g.Key,
                 count = g.Count()
              }).First();

Console.WriteLine("El mayor es {0} y se repite {1} veces", result.item, result.count);

When you evaluate repeated you do not have a single index to report because there are several values, so what you report is what the item is and the number of times it is repeated

    
answered by 06.12.2018 в 02:47