Dave Lim
Dave Lim

Reputation: 403

How to find array index of largest value?

The title above sums up my question, to clarify things an example is:

array[0] = 1
array[1] = 3
array[2] = 7  // largest
array[3] = 5

so the result I would like is 2, since it contains the largest element 7.

Upvotes: 16

Views: 87526

Answers (9)

Stuepfnick
Stuepfnick

Reputation: 79

Would do it like this (as I don't know any predefined function to get the index of highest element, only the element itself, of course you could get the index with list.indexOf(element) then, but array needs to be converted to list and 2 iterations):

maxIndex = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] > array[maxIndex]) {
        maxIndex = i;
    }
}

Upvotes: 0

David Lilljegren
David Lilljegren

Reputation: 1949

Another functional implementation

int array[] = new int[]{1,3,7,5};        

int maxIndex =IntStream.range(0,array.length)
              .boxed()
              .max(Comparator.comparingInt(i -> array[i]))
              .map(max->array[max])
              .orElse(-1);

Upvotes: 1

Pratik Pawar
Pratik Pawar

Reputation: 91

Please find below code for the same

Integer array[] = new Integer[4];
array[0] = 1;
array[1] = 3;
array[2] = 7;
array[3] = 5;

List < Integer > numberList = Arrays.asList(array);

int index_maxNumber = numberList.indexOf(Collections.max(numberList));

System.out.println(index_maxNumber);

Upvotes: 2

user12057507
user12057507

Reputation:

Two lines code will do that in efficient way

//find the maximum value using stream API of the java 8

Integer max =Arrays.stream(numbers) .max(Integer::compare).get();
// find the index of that value
int index  = Arrays.asList(numbers).indexOf(max);

Upvotes: 1

kimalser
kimalser

Reputation: 323

Using Java 8 streams:

    List<Integer> list = Arrays.asList(1, 3, 7, 5);
    IntStream.range(0, list.size())
            .reduce((i, j) -> list.get(i) > list.get(j) ? i : j)
            .getAsInt();

Upvotes: 3

Eric
Eric

Reputation: 1297

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
}

Upvotes: 15

Shekhar Khairnar
Shekhar Khairnar

Reputation: 2691

one way will be:

 Integer[] array = new Integer[4];
    array[0] = 1;
    array[1] = 3;
    array[2] = 7;
    array[3] = 5;

    List<Integer> iList = Arrays.asList(array);
    System.out.println(iList.indexOf(Collections.max(iList)));
    System.out.println(iList.indexOf(Collections.min(iList)));

Upvotes: 5

ifloop
ifloop

Reputation: 8386

int maxAt = 0;

for (int i = 0; i < array.length; i++) {
    maxAt = array[i] > array[maxAt] ? i : maxAt;
}

Upvotes: 25

Boris Brodski
Boris Brodski

Reputation: 8715

public int getIndexOfMax(int array[]) {
    if (array.length == 0) {
        return -1; // array contains no elements
    }
    int max = array[0];
    int pos = 0;

    for(int i=1; i<array.length; i++) {
        if (max < array[i]) {
            pos = i;
            max = array[i];
        }
    }
    return pos;
}

Upvotes: 2

Related Questions