Array: Find the lowest index of the largest element in an array

Q) Find the lowest index of the largest element in an array

Example:

Array: 1,5,2,3,5,5

Output:
Lowest Index of max element= 2

public class ArrayMaxElementLowestIndex {
  
  public int lowestIndex (int [] sampleArray){
    int index=0;//intitalize the index to zero
    for (int i=0; i<sampleArray.length; i++){ //running a loop through the length of the array
      if(sampleArray[i]>sampleArray[index]){ //compare each element to the element at "index"
    	index= i;//if above condition if fulfilled replace index with i
      }
    }
    return (index+1); 
  }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: