Find largest and smallest number in an array in C#

Find largest and smallest number in an array in C#

To find the largest and smallest number in an array in C#, you can use the Max and Min methods from LINQ or implement a simple loop to iterate through the array and keep track of the maximum and minimum values. Here are both approaches:

  • Using LINQ's Max and Min methods:
using System; using System.Linq; public class Program { public static void Main() { int[] numbers = { 10, 5, 20, 15, 30 }; // Find the largest number using LINQ Max int largestNumber = numbers.Max(); // Find the smallest number using LINQ Min int smallestNumber = numbers.Min(); Console.WriteLine($"Largest number: {largestNumber}"); Console.WriteLine($"Smallest number: {smallestNumber}"); } } 
  • Using a simple loop:
using System; public class Program { public static void Main() { int[] numbers = { 10, 5, 20, 15, 30 }; // Find the largest and smallest numbers using a loop int largestNumber = numbers[0]; int smallestNumber = numbers[0]; for (int i = 1; i < numbers.Length; i++) { if (numbers[i] > largestNumber) { largestNumber = numbers[i]; } if (numbers[i] < smallestNumber) { smallestNumber = numbers[i]; } } Console.WriteLine($"Largest number: {largestNumber}"); Console.WriteLine($"Smallest number: {smallestNumber}"); } } 

Both approaches will give you the same result:

Largest number: 30 Smallest number: 5 

You can choose the approach that suits your code style and requirements. The LINQ approach is more concise and expressive, while the loop approach might be preferred in performance-critical scenarios as it requires only a single pass through the array.

Examples

  1. C# find largest and smallest number in array

    Description: This query aims to find both the largest and smallest numbers in an array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(int[] array, out int min, out int max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the largest and smallest numbers in an array in C# using a loop.

  2. C# find largest and smallest number in integer array

    Description: This query seeks to find the largest and smallest numbers in an integer array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(int[] array, out int min, out int max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the largest and smallest numbers in an integer array in C# using a loop.

  3. C# find smallest and largest number in array

    Description: This query aims to find both the smallest and largest numbers in an array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(int[] array, out int min, out int max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the smallest and largest numbers in an array in C# using a loop.

  4. C# find largest and smallest value in array

    Description: This query seeks to find both the largest and smallest values in an array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(int[] array, out int min, out int max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the largest and smallest values in an array in C# using a loop.

  5. C# find maximum and minimum number in array

    Description: This query aims to find both the maximum and minimum numbers in an array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(int[] array, out int min, out int max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the maximum and minimum numbers in an array in C# using a loop.

  6. C# find largest and smallest number in float array

    Description: This query seeks to find both the largest and smallest numbers in a float array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(float[] array, out float min, out float max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the largest and smallest numbers in a float array in C# using a loop.

  7. C# find smallest and largest number in double array

    Description: This query aims to find both the smallest and largest numbers in a double array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(double[] array, out double min, out double max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the smallest and largest numbers in a double array in C# using a loop.

  8. C# find smallest and largest number in decimal array

    Description: This query aims to find both the smallest and largest numbers in a decimal array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(decimal[] array, out decimal min, out decimal max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the smallest and largest numbers in a decimal array in C# using a loop.

  9. C# find largest and smallest number in byte array

    Description: This query seeks to find both the largest and smallest numbers in a byte array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(byte[] array, out byte min, out byte max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the largest and smallest numbers in a byte array in C# using a loop.

  10. C# find largest and smallest number in long array

    Description: This query aims to find both the largest and smallest numbers in a long array in C#.

    using System; public class ArrayMinMaxFinder { public static void FindMinMax(long[] array, out long min, out long max) { min = max = array[0]; for (int i = 1; i < array.Length; i++) { if (array[i] < min) min = array[i]; else if (array[i] > max) max = array[i]; } } } 

    Description: This code snippet provides a method to find both the largest and smallest numbers in a long array in C# using a loop.


More Tags

dojo-1.8 sonar-runner uitextfield categories destructuring spring-amqp logstash-configuration euro full-text-indexing codenameone

More C# Questions

More Geometry Calculators

More Entertainment Anecdotes Calculators

More Other animals Calculators

More Financial Calculators