Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to find maximum and minimum element in an array
To find the maximum and minimum elements in an array in C#, we initialize both values to the first element of the array and then compare each subsequent element. This approach ensures we correctly identify the extreme values regardless of the array's content.
Algorithm
The algorithm follows these steps −
Initialize both max and min variables to the first array element.
Iterate through the array starting from the second element (index 1).
Compare each element with the current max and min values.
Update max and min accordingly when a larger or smaller element is found.
Syntax
The basic comparison logic for finding maximum element −
if(arr[i] > max) {
max = arr[i];
}
The basic comparison logic for finding minimum element −
if(arr[i] < min) {
min = arr[i];
}
Using Simple Loop Approach
Example
using System;
public class Demo {
public static void Main() {
int[] arr = new int[5] {99, 95, 93, 89, 87};
int i, max, min, n;
// size of the array
n = 5;
max = arr[0];
min = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
if(arr[i] < min) {
min = arr[i];
}
}
Console.WriteLine("Maximum element = {0}", max);
Console.WriteLine("Minimum element = {0}", min);
}
}
The output of the above code is −
Maximum element = 99 Minimum element = 87
Using LINQ Methods
C# provides built-in LINQ methods Max() and Min() for finding maximum and minimum values −
Example
using System;
using System.Linq;
public class Demo {
public static void Main() {
int[] arr = {15, 42, 8, 73, 29, 91, 5};
int max = arr.Max();
int min = arr.Min();
Console.WriteLine("Array: [{0}]", string.Join(", ", arr));
Console.WriteLine("Maximum element = {0}", max);
Console.WriteLine("Minimum element = {0}", min);
}
}
The output of the above code is −
Array: [15, 42, 8, 73, 29, 91, 5] Maximum element = 91 Minimum element = 5
Finding Maximum and Minimum with Positions
This example shows how to find both the values and their positions in the array −
Example
using System;
public class Demo {
public static void Main() {
int[] arr = {25, 11, 7, 75, 56};
int max = arr[0], min = arr[0];
int maxIndex = 0, minIndex = 0;
for(int i = 1; i < arr.Length; i++) {
if(arr[i] > max) {
max = arr[i];
maxIndex = i;
}
if(arr[i] < min) {
min = arr[i];
minIndex = i;
}
}
Console.WriteLine("Array: [{0}]", string.Join(", ", arr));
Console.WriteLine("Maximum element = {0} at index {1}", max, maxIndex);
Console.WriteLine("Minimum element = {0} at index {1}", min, minIndex);
}
}
The output of the above code is −
Array: [25, 11, 7, 75, 56] Maximum element = 75 at index 3 Minimum element = 7 at index 2
Comparison of Approaches
| Approach | Time Complexity | Advantages | Best Use Case |
|---|---|---|---|
| Simple Loop | O(n) | No additional memory, finds both in single pass | When you need both max and min |
| LINQ Methods | O(n) each | Clean, readable code | Quick operations, small arrays |
| With Positions | O(n) | Returns values and their locations | When index information is needed |
Conclusion
Finding maximum and minimum elements in an array can be accomplished using simple iteration or LINQ methods. The loop approach is efficient for finding both values in a single pass, while LINQ provides cleaner code for straightforward operations.
