Good night,
I have to get the highest number of a list and there is no way to get it, I am using 2 for to get it but the last highest number is saved, I can't do it. This is my code:
static int get(int[] a)
{
int sort = 0;
int sort2 = 0;
int mayor = 0;
List<int> a1 = new List<int>();
foreach (int test in a)
{
a1.Add(test);
}
int cont = a1.Count;
for (int i = 0; i < cont; i++)
{
for (int k = 0; k < cont; k++)
{
sort = a1[i];
sort2 = a1[k];
if (sort > sort2)
{
mayor = sort;
}
else
{
mayor = sort2;
}
}
}
return mayor;
Good day
In case it is not necessary
for
that it is not an exercise using that cycle , addusing System;
andusing System.Linq;
and use theMax
.Here you can see it better Max and Min
Of course, in the above, it is necessary to validate if it has data
Clever,
The second for was too much, I only had to iterate once and save the largest in my variable as follows