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
  • 330,190
  • 66
  • 504
  • 555
user3071909
  • 15
  • 3
  • 3
  • 8

5 Answers5

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,207
  • 5
  • 41
  • 51
3

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,003
  • 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
  • 12,288
  • 14
  • 67
  • 121
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
  • 435
  • 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
  • 5,799
  • 7
  • 29
  • 54