Java: Finding the Highest Value in an Array

Java: Finding the highest value in an array

It's printing out a number every time it finds one that is higher than the current max (which happens to occur three times in your case.) Move the print outside of the for loop and you should be good.

for (int counter = 1; counter < decMax.length; counter++)
{
if (decMax[counter] > max)
{
max = decMax[counter];
}
}

System.out.println("The highest maximum for the December is: " + max);

Finding the max/min value in an array of primitives using Java

Using Commons Lang (to convert) + Collections (to min/max)

import java.util.Arrays;
import java.util.Collections;

import org.apache.commons.lang.ArrayUtils;

public class MinMaxValue {

public static void main(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};

List b = Arrays.asList(ArrayUtils.toObject(a));

System.out.println(Collections.min(b));
System.out.println(Collections.max(b));
}
}

Note that Arrays.asList() wraps the underlying array, so it should not be too memory intensive and it should not perform a copy on the elements of the array.

traversing, finding the max value and finding average of arrays in java

I think there can be multiple ways to do. If arrays is the only data structure allowed and using the "hints given" to follow the existing code fragment, I think the algorithm can be:

  1. Store the first element in variable result.

  2. Then loop through the array and compare if the current element is larger than the variable result. If it is, then store it in variable result.

  3. The highest value will be in variable result after the iteration.

     result = arr[0];
    mark_avg = 0;
    for (int e : arr) {
    if (e > result) {
    result = e;
    }
    mark_avg += e;
    }

    mark_avg = mark_avg / arr.length;

    System.out.println("Highest mark is " + result);
    System.out.println("Average mark is " + mark_avg);

how can I get the index of the max value in an array?

If you do not want to use another class you can do the below.
You need to store both the max and the index of where max was so that you can also print the city

String[] city = getCities ();  // assuming this code is done
int[] temp = getTemparatures ();
int max = Integer.MIN_VALUE;
int index = -1;

for(int i = 0; i < temp.length; i ++){
if(max < temp[i]){
max = temp[i];
index = i;
}
}

System.out.println ("The city with the highest average temperature is " + city[index] +
" with an average temperature of " + temp[index]);

Finding the highest value in an array, but what if the highest value exists two or more places in the array?

You can return a List<Integer> having the locations/indexes where the max value was found.

As you found the max use it to find the indexes where it was present in the array:

public List<Integer> maxValue(int array[]){
int max = Arrays.stream(array).max().orElse(-1);
return IntStream.range(0, array.length)
.filter(idx -> array[idx] == max)
.mapToObj(i -> i)
.collect(Collectors.toList());
}

How to find array index of largest value?

public int getIndexOfLargest( int[] array )
{
if ( array == null || array.length == 0 ) return -1; // null or empty

int largest = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[largest] ) largest = i;
}
return largest; // position of the first largest found
}

How to iterate through array and find highest value: Java

You don't actually need to keep track of the chosen month for every iteration. Assuming months are related to the array elements by the index, all you need is to find out the index of the greatest element - you don't even need to track the greatest value:

public class HelloWorld {
static int[] array = {3, 6, 7, 3, 2, 30, 9, 13, 12, 1, 2, 1};
static String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

public static void main(String[] args) {
int index = 0;
for (int i = 1; i < array.length; i++)
if (array[index] < array[i])
index = i;
System.out.println(array[index] + " greatest month is: " + month[index]);
}
}

How to find min max values in Array - Scanner class

public static void main(String... args) {           
Scanner scan = new Scanner(System.in);
System.out.print("How many numbers in a set: ");

int[] arr = new int[scan.nextInt()];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

System.out.print("Input your numbers: ");

for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
min = Math.min(min, arr[i]);
max = Math.max(max, arr[i]);
}

System.out.println(Arrays.toString(arr));
System.out.println(max + " " + min);
System.out.println();
}


Related Topics



Leave a reply



Submit