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.
Duncan Jones
69.6k32 gold badges202 silver badges260 bronze badges
asked Apr 7, 2014 at 11:55
-
8What you have tried so far?Abimaran Kugathasan– Abimaran Kugathasan2014年04月07日 11:56:25 +00:00Commented Apr 7, 2014 at 11:56
-
2possible duplicate of How to find the largest int in an array using javarobertoia– robertoia2014年04月07日 11:57:24 +00:00Commented Apr 7, 2014 at 11:57
-
1This is a trivial thing to do actually. There are a lot of searching algorithms for this, have you even tried something?RB-Develop– RB-Develop2014年04月07日 11:57:25 +00:00Commented Apr 7, 2014 at 11:57
-
1there are simpler (and quicker) things than sorting...Betlista– Betlista2014年04月07日 11:58:54 +00:00Commented Apr 7, 2014 at 11:58
-
3Down-vote from me - I expect to see some effort on your part. This is not a difficult challenge.Duncan Jones– Duncan Jones2014年04月07日 12:01:10 +00:00Commented Apr 7, 2014 at 12:01
9 Answers 9
int maxAt = 0;
for (int i = 0; i < array.length; i++) {
maxAt = array[i] > array[maxAt] ? i : maxAt;
}
answered Apr 7, 2014 at 12:00
-
@Duncan Thanks for the hint, fixed thisifloop– ifloop2014年04月07日 12:08:24 +00:00Commented Apr 7, 2014 at 12:08
-
@ifLoop, it's not fixed. The first iteration of the loop will crash with an exception (-1 out of range) ... edit: I see that you've corrected that now.Eric– Eric2014年04月07日 12:10:42 +00:00Commented Apr 7, 2014 at 12:10
-
@Duncan, sorry I did get you and the author mixed up - my bad.Eric– Eric2014年04月07日 12:14:57 +00:00Commented Apr 7, 2014 at 12:14
-
4It might be neatest, if you would start your loop with
1
:) Also you can't differentiate between empty array and one element array.Boris Brodski– Boris Brodski2014年04月07日 12:51:04 +00:00Commented Apr 7, 2014 at 12:51 -
As suggested by Boris, starting at one makes sense.Tarik– Tarik2019年10月31日 18:37:02 +00:00Commented Oct 31, 2019 at 18:37
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
}
answered Apr 7, 2014 at 12:05
-
1You don't need to store
largest
.Duncan Jones– Duncan Jones2014年04月07日 12:07:32 +00:00Commented Apr 7, 2014 at 12:07 -
1@Duncan, true but it is a little more "readable" when you do ;)Eric– Eric2014年04月07日 12:09:13 +00:00Commented Apr 7, 2014 at 12:09
-
1Agree to disagree :-) I think
indexOfLargest
would suffice as a single, self-explanatory variable.Duncan Jones– Duncan Jones2014年04月07日 12:09:46 +00:00Commented Apr 7, 2014 at 12:09 -
1I wouldn't have thought so. For the avoidance of doubt, this is what I'm suggesting (copy into IDE to format nicely!):
int indexOfLargest = 0; for ( int i = 0; i < array.length; i++ ) { if ( array[i] > array[indexOfLargest] ) { indexOfLargest = i; }}
. Basically the same as ifLoop's answer.Duncan Jones– Duncan Jones2014年04月07日 12:17:08 +00:00Commented Apr 7, 2014 at 12:17 -
2@Duncan I would start the loop with
1
, not0
.Boris Brodski– Boris Brodski2014年04月07日 12:47:21 +00:00Commented Apr 7, 2014 at 12:47
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)));
answered Apr 7, 2014 at 12:08
-
This does not find the index.Duncan Jones– Duncan Jones2014年04月07日 12:10:49 +00:00Commented Apr 7, 2014 at 12:10
-
1This is a nice solution if the array was already of
Integer
s. Sadly in many cases it's not and there isn't a pleasant way to move between the two (without external libs).Duncan Jones– Duncan Jones2014年04月07日 12:29:02 +00:00Commented Apr 7, 2014 at 12:29 -
what if the values are repeatable.Jafar Ali– Jafar Ali2018年05月09日 21:03:05 +00:00Commented May 9, 2018 at 21:03
-
If the values are repeatable the first occurence of index with repeated value will be considered.Shekhar Khairnar– Shekhar Khairnar2018年05月14日 11:30:17 +00:00Commented May 14, 2018 at 11:30
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();
answered Oct 11, 2019 at 21:46
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;
}
answered Apr 7, 2014 at 12:05
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);
zmag
8,27112 gold badges39 silver badges47 bronze badges
answered Dec 6, 2019 at 12:26
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);
answered Oct 11, 2019 at 22:03
user12057507user12057507
-
1The most elegant solution so far but due to Java streams implementation, not very efficient. C# does a better job with the IEnumerable interface implemented by arrays.Tarik– Tarik2019年10月31日 18:34:46 +00:00Commented Oct 31, 2019 at 18:34
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);
answered Dec 6, 2019 at 13:16
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;
}
}
lang-java