Largest element in a list

Write a function that returns the largest element in a list.

Solution:


using System;

namespace LargestElements
{
    class Program
    {
        private static int inputDigit = 5;
        public static void Main()
        {
            int[] arr = { 2, 3, 5, 5, 6, 7, 9 };

            Console.WriteLine(LargestElement(arr));

            Console.ReadLine();
        }


        static int LargestElement(int[] list)
        {
            if (list.Length == 0)
            {
                throw new InvalidOperationException("Empty list");
            }

            var maxAge = 0;
            foreach (var i in list)
            {
                if (i > maxAge) maxAge = i;
            }

            return maxAge;
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *