To find the maximum element in an array, you need to examine each element in the array and determine which one has the highest value. Here’s a step-by-step explanation with examples and code in multiple languages.
Step-by-Step Explanation
- Initialize the Maximum Value: Start by assuming the first element of the array is the maximum value.
- Iterate Through the Array: Go through each element in the array one by one.
- Compare Each Element: For each element, check if it is greater than the current maximum value.
- Update the Maximum Value: If you find a greater element, update the maximum value.
- Finish the Iteration: After checking all elements, the maximum value will be the largest element in the array.
Example
Let’s find the maximum value in the following array: [4, 12, 7, 9, 1]
.
- Initialize: Assume the maximum value is the first element,
4
. - Iterate:
- Compare
4
with12
. Since12
is greater, update the maximum to12
. - Compare
12
with7
. No change since12
is greater. - Compare
12
with9
. No change since12
is greater. - Compare
12
with1
. No change since12
is greater.
- Result: The maximum value is
12
.
Code Examples
Python
def find_maximum(arr):
# Step 1: Assume the first element is the maximum
max_value = arr[0]
# Step 2: Iterate through the array
for num in arr:
# Step 3 & 4: Compare and update if necessary
if num > max_value:
max_value = num
return max_value
# Example usage
array = [4, 12, 7, 9, 1]
print("The maximum value is:", find_maximum(array))
JavaScript
function findMaximum(arr) {
// Step 1: Assume the first element is the maximum
let maxValue = arr[0];
// Step 2: Iterate through the array
for (let i = 0; i < arr.length; i++) {
// Step 3 & 4: Compare and update if necessary
if (arr[i] > maxValue) {
maxValue = arr[i];
}
}
return maxValue;
}
// Example usage
const array = [4, 12, 7, 9, 1];
console.log("The maximum value is:", findMaximum(array));
Java
public class Main {
public static int findMaximum(int[] arr) {
// Step 1: Assume the first element is the maximum
int maxValue = arr[0];
// Step 2: Iterate through the array
for (int i = 1; i < arr.length; i++) {
// Step 3 & 4: Compare and update if necessary
if (arr[i] > maxValue) {
maxValue = arr[i];
}
}
return maxValue;
}
public static void main(String[] args) {
int[] array = {4, 12, 7, 9, 1};
System.out.println("The maximum value is: " + findMaximum(array));
}
}
C++
#include <iostream>
using namespace std;
int findMaximum(int arr[], int size) {
// Step 1: Assume the first element is the maximum
int maxValue = arr[0];
// Step 2: Iterate through the array
for (int i = 1; i < size; i++) {
// Step 3 & 4: Compare and update if necessary
if (arr[i] > maxValue) {
maxValue = arr[i];
}
}
return maxValue;
}
int main() {
int array[] = {4, 12, 7, 9, 1};
int size = sizeof(array) / sizeof(array[0]);
cout << "The maximum value is: " << findMaximum(array, size) << endl;
return 0;
}
To find the maximum element in an array:
- Initialize the first element as the maximum.
- Iterate through the array and compare each element with the current maximum.
- Update the maximum if a larger element is found.
- After the loop, the maximum variable holds the largest value.