Have a max int and set it to the first value in the array. Then in a for loop iterate through the whole array and see if the max int is larger than the int at the current index.
int max = array.get(0);
for (int i = 1; i < array.length; i++) {
if (array.get(i) > max) {
max = array.get(i);
}
}
Answer from Philip on Stack OverflowHow to find maximum value of an Array
java - How to find the maximum value in an array? - Stack Overflow
Finding the max value of a property in an array of objects
Help With Function to find MAX value in Array
I want to know if there's any shortcut/in-built function to find the max value in an integer array
I have checked stackoverflow but the only answer is find is to convert the array into a list and then find out the max value.
I was wondering if there is a way to find out the max value from the array itself instead of writing code to find the largest item in the array .
Have a max int and set it to the first value in the array. Then in a for loop iterate through the whole array and see if the max int is larger than the int at the current index.
int max = array.get(0);
for (int i = 1; i < array.length; i++) {
if (array.get(i) > max) {
max = array.get(i);
}
}
If you can change the order of the elements:
int[] myArray = new int[]{1, 3, 8, 5, 7, };
Arrays.sort(myArray);
int max = myArray[myArray.length - 1];
If you can't change the order of the elements:
int[] myArray = new int[]{1, 3, 8, 5, 7, };
int max = Integer.MIN_VALUE;
for(int i = 0; i < myArray.length; i++) {
if(myArray[i] > max) {
max = myArray[i];
}
}
To find the maximum y value of the objects in array:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
or in more modern JavaScript:
Math.max(...array.map(o => o.y))
Warning:
This method is not advisable, it is better to use reduce. With a large array, Math.max will be called with a large number of arguments, which can cause stack overflow.
Find the object whose property "Y" has the greatest value in an array of objects
One way would be to use Array reduce..
const max = data.reduce(function(prev, current) {
return (prev && prev.y > current.y) ? prev : current
}) //returns object
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
- http://caniuse.com/#search=reduce (IE9 and above)
If you don't need to support IE (only Edge), or can use a pre-compiler such as Babel you could use the more terse syntax.
const max = data.reduce((prev, current) => (prev && prev.y > current.y) ? prev : current)
Note that if you have multiple objects sharing the same max value, it will only return the first matched object.
So I made a function to determine the maximum element of an array of integers. But when I try to print the value of the maximum element, it gives me a weird large number 1619117764., and isn't what I'm expecting. What am I doing wrong? Thank you so much in advance.
This is the error message:
C:\Users\USER\Desktop\Discrete Math\Max Value.c|25|warning: passing argument 1 of 'max_value' makes pointer from integer without a cast [-Wint-conversion]|
Here is my code:
#include <stdio.h>
int max_value (int input[])
{
int size=10, max;
max=input[0];
for(int i=1;i<size;i++)
{
if(max<input[i])
{
max = input[i];
}
}
return max;
}
int main ()
{
int x, max;
int input[] = {2, 3, 5, 10, 15, 42, 28, 88, 92};
x = max_value(max);
printf("/n The Max is: %d" ,x);
return 0;
}