How to find Maximum and Minimum values of a list in C#?

I’m taking this exercise from CodeWars [https://www.codewars.com/kata/577a98a6ae28071780000989/train/csharp]. > Your task is to make two functions ( max and min, or maximum and minimum, etc., depending on the language ) that receive a list of integers as input, and return the largest and lowest number in that list, respectively. Examples (Input -> Output) * [4,6,2,1,9,63,-134,566] -> max = 566, min = -134 * [-52, 56, 30, 29, -54, 0, -110] -> min = -110, max = 56 * [42, 54, 65, 87, 0]

Abdallah Yashir
1 min readMar 29, 2023

I’m taking this exercise from CodeWars.

Your task is to make two functions ( max and min, or maximum and minimum, etc., depending on the language ) that receive a list of integers as input, and return the largest and lowest number in that list, respectively.

Examples (Input -> Output)

* [4,6,2,1,9,63,-134,566] -> max = 566, min = -134
* [-52, 56, 30, 29, -54, 0, -110] -> min = -110, max = 56
* [42, 54, 65, 87, 0] -> min = 0, max = 87
* [5] -> min = 5, max = 5

Potential solution(s) I’m thinking of:

  • you can sort the array and get the first item
  • sort the list, reverse it and get the first item
  • compare each item and have an intermediary value
  • or use the built-in functions

​Eventually, it’s way easier to just use the built-in methods.

list.Min();
list.Max();

--

--

Abdallah Yashir
Abdallah Yashir

Written by Abdallah Yashir

Senior Software Developer, Writer, Amateur Photographer, Reader

No responses yet