0

I am supposed to write a program that finds the largest index in an unsorted array of integers.

Use a method that takes an array of integers as a parameter. The method should search the array and return the index of the largest value.

So i wrote a program that returns the highest value, for example it returns 13 as the highest number instead of the index of 2. So how would i return the index instead of the highest number itself? If that is an easy fix, does the rest of my code look correct? Thanks!

public class LargestInteger 
{
    public static void main(String[] args)
    {
        int[] largeArray = {5,4,13,7,7,8,9,10,5};

        System.out.println(findLargest(largeArray));
    }

    public static int findLargest(int array[])
    {
        int largest = array[0];

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

        return largest;
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user3071909
  • 15
  • 3
  • 3
  • 9
  • 1
    if you want to return `index` only, simply change `largest = array[i]; ` to `largest = i;` – Baby Feb 11 '14 at 03:47
  • Relevent: http://stackoverflow.com/questions/1522108/how-to-find-the-index-of-an-element-in-an-array-in-java – Jordan.J.D Feb 11 '14 at 03:48
  • Be careful with your wording. What you asked is NOT what the problem statement you pasted says. – Alex Feb 11 '14 at 03:51
  • @RhinoFeeder an example of [XyProblem](http://mywiki.wooledge.org/XyProblem) – Baby Feb 11 '14 at 03:53
  • @RhinoFeeder How is it not? – user3071909 Feb 11 '14 at 03:53
  • You are telling us you want the highest array index (array.length-1). But what you actually want is the index containing the highest value. They are two different hings. – Alex Feb 11 '14 at 03:54

6 Answers6

3

Can also done with,

int[] largeArray     = {5,4,13,7,7,8,9,10,5};
int   largestElement = findLargest(largeArray);
int   index          = Arrays.asList(5,4,13,7,7,8,9,10,5).indexOf(largestElement);
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
2

In java 8 you can do it simply:

To get the highest:

int[] largeArray = {5,4,13,7,7,8,9,10,5};
System.out.println(IntStream.of(largeArray).max().getAsInt());

result: 13

And to sum them:

System.out.println(IntStream.of(largeArray).sum());

result: 68

Gabriel
  • 2,011
  • 14
  • 14
1

You need to keep and store the largest index. Try this:

public static int findLargest(int array[])
{
    int largest = array[0];
    int largestIndex = 0;

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

    return largestIndex;
}
Jason
  • 13,563
  • 15
  • 74
  • 125
0

Create a for loop at the end of the method, and a variable like index. Search through the list and find if the variable matches, remembering to increment the index variable every time.

Example:

int index = 0;
for (int i : array)
{
    if (i == largest)
    {
         break;
    }
    index++;
}
return index;
Arbiter
  • 433
  • 2
  • 9
0

You can add aditional method that gets largest number and an array then search for index of that number:

public class LargestInteger 
{
    public static void main(String[] args)
    {
        int[] largeArray = {5,4,13,7,7,8,9,10,5};

        System.out.println(indexOfLargest(largeArray, findLargest(largeArray)));
    }

    public static int findLargest(int array[])
    {
        int largest = array[0];

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

        return largest;
    }

    public static int indexOfLargest(int array[], int number)
    {

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

        return -1;
    }

}
msmolcic
  • 6,407
  • 8
  • 32
  • 56
0

Just thought we need a simple answer in the era of stream interface. If your array is int[] a, you can get argmax (the index of the largest value) with the following code:

IntStream.range(0, a.length).boxed().max(Comparator.comparing(i->a[i])).get();

If you want argmin, just change max() to min(). Because it compares elements in their natural order, any array with comparable elements will do, like double[] or long[].

ccymus
  • 151
  • 1
  • 7