Anel Hodžić Anel Hodžić - 2 months ago 8
C# Question

Compare one number in array with others in same

Lets say i have an array with 10 elements in it. I want to compare the first number is it the biggest in that same array.

Is there any way to make it without linq.

Numbers are 2 4 6 7 8 9 3

I want to know if the first number is biggest or equal in array

Answer

You should iterate over the array and check if there is at least one element that is bigger then the first element.

var array = new [] { 2, 4, 6, 7, 8, 9, 3 };

int element = array[0];
bool isBiggestOrEqual = true;
for (int i = 1; i < array.Length; i++)
{
    if (element < array[i])
    {
        isBiggestOrEqual = false;
        break;
    }
}

Console.WriteLine("Is {0} the biggest or equal to biggest in array ? {1}", element, isBiggestOrEqual);
Comments