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;
    }
}
6
  • 1
    if you want to return index only, simply change largest = array[i]; to largest = i; Commented Feb 11, 2014 at 3:47
  • Relevent: stackoverflow.com/questions/1522108/… Commented Feb 11, 2014 at 3:48
  • Be careful with your wording. What you asked is NOT what the problem statement you pasted says. Commented Feb 11, 2014 at 3:51
  • @RhinoFeeder an example of XyProblem Commented Feb 11, 2014 at 3:53
  • @RhinoFeeder How is it not? Commented Feb 11, 2014 at 3:53

6 Answers 6

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);
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

The OPs question was to find the index, not the max value (although the code posted by the OP is incorrect).
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;
}

Comments

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;

Comments

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;
    }

}

Comments

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[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.