Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created June 15, 2021 15:58
Show Gist options
  • Save bijay-shrestha/52cfbdd39044adf025e174dd5917a173 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/52cfbdd39044adf025e174dd5917a173 to your computer and use it in GitHub Desktop.
/**
* * Get Maximum Element value from the given array.
*/
package com.basic.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MaxNumberInArray {
public static void main(String[] args) {
int[] arrayOfNumbers = {2, 10, 5, -8, 8, 11, 15, 3};
log.info("Max value from an array {} is {}", arrayOfNumbers, getMaxNumberInArray(arrayOfNumbers));
}
static int getMaxNumberInArray(int[] a) {
int len = a.length;
int temp = a[0];
int max = 0;
for (int i = 0; i < len; i++) {
if (temp >= a[i]) {
max = temp;
} else {
max = a[i];
}
if (temp >= max) {
max = temp;
} else {
temp = max;
}
}
return max;
}
}
@sanjmgr
Copy link

sanjmgr commented Aug 26, 2022

private static int maxNumber(int[] arrayOfNumbers) {
    if (arrayOfNumbers.length < 1) {
        return 0;
    }

    int max = 0;
    for (int number : arrayOfNumbers) {
        if (number > max) {
            max = number;
        }
    }

    return max;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment