Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 699 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding the max/min value in an array of primitives using Java

#11
I have a little helper class in all of my applications with methods like:

public static double arrayMax(double[] arr) {
double max = Double.NEGATIVE_INFINITY;

for(double cur: arr)
max = Math.max(max, cur);

return max;
}
Reply

#12
public int getMin(int[] values){
int ret = values[0];
for(int i = 1; i < values.length; i++)
ret = Math.min(ret,values[i]);
return ret;
}
Reply

#13
Here is a solution to get the max value in about 99% of runs (change the 0.01 to get a better result):

public static double getMax(double[] vals){
final double[] max = {Double.NEGATIVE_INFINITY};

IntStream.of(new Random().ints((int) Math.ceil(Math.log(0.01) / Math.log(1.0 - (1.0/vals.length))),0,vals.length).toArray())
.forEach(r -> max[0] = (max[0] < vals[r])? vals[r]: max[0]);

return max[0];
}
(Not completely serious)
Reply

#14
A solution with `reduce()`:

int[] array = {23, 3, 56, 97, 42};
// directly print out
Arrays.stream(array).reduce((x, y) -> x > y ? x : y).ifPresent(System.out::println);

// get the result as an int
int res = Arrays.stream(array).reduce((x, y) -> x > y ? x : y).getAsInt();
System.out.println(res);
>>
97
97

In the code above, `reduce()` returns data in `Optional` format, which you can convert to `int` by `getAsInt()`.

If we want to compare the max value with a certain number, we can set a start value in `reduce()`:

int[] array = {23, 3, 56, 97, 42};
// e.g., compare with 100
int max = Arrays.stream(array).reduce(100, (x, y) -> x > y ? x : y);
System.out.println(max);
>>
100

In the code above, when `reduce()` with an identity (start value) as the first parameter, it returns data in the same format with the identity. With this property, we can apply this solution to other arrays:

double[] array = {23.1, 3, 56.6, 97, 42};
double max = Arrays.stream(array).reduce(array[0], (x, y) -> x > y ? x : y);
System.out.println(max);
>>
97.0
Reply

#15
Here's a utility class providing `min/max` methods for primitive types: [Primitives.java](

[To see links please register here]

)

int [] numbers= {10,1,8,7,6,5,2};
int a=Integer.MAX_VALUE;
for(int c:numbers) {
a=c<a?c:a;
}

System.out.println("Lowest value is"+a);
Reply

#16
int[] arr = {1, 2, 3};

List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
int max_ = Collections.max(list);
int i;
if (max_ > 0) {
for (i = 1; i < Collections.max(list); i++) {
if (!list.contains(i)) {
System.out.println(i);
break;
}
}
if(i==max_){
System.out.println(i+1);
}
} else {
System.out.println("1");
}
}
Reply

#17
By sorting the array, you get the first and last values for min / max.

import java.util.Arrays;

public class apples {

public static void main(String[] args) {
int a[] = {2,5,3,7,8};
Arrays.sort(a);

int min =a[0];
System.out.println(min);

int max= a[a.length-1];
System.out.println(max);
}

}

Although the sorting operation is more expensive than simply finding min/max values with a simple loop. But when performance is not a concern (e.g. small arrays, or your the cost is irrelevant for your application), it is a quite simple solution.

Note: the array also gets modified after this.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through