How to find largest element in an array with index and value in Java
public class FindHighestElement{
public static void main(String[] args) {
int [] array={1,9,11,4,3};
int arrayValue=0;
int arrayMatch=0;
int count=0;
for(int i=0;i<array.length;i++){
arrayValue=array[i];
for(int j=0;j<array.length;j++){
arrayMatch=array[j];
if(arrayValue>arrayMatch){
count++;
}
}
if(count==(array.length-1)){
System.out.println("The highest Value : "+arrayValue);
System.out.println("The index is : " +i);
}
count=0;
}
}
}
Output:
The highest Value : 11
The index is : 2
Comments
Post a Comment