0
1.9kviews
Write a program to find largest and second largest element of array.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 10 M

Year: May 2014

1 Answer
0
26views
        public static void main(String[] arguments) {
       int[] times = { 341, 273, 278, 329, 445, 100, 388, 275, 243, 500}; //declaring array
        secondLargest(times);   //calling method
    }
private static void secondLargest(int arr[]){
            int maxOne=arr[0];
            int maxTwo=arr[1];
            for(int i=0;i<arr.length;i++){
                if(arr[i]>maxOne)
        {
                    maxTwo=maxOne;
                    maxOne=arr[i];
                }
        else if (arr[i]>maxTwo)
            {            
            maxTwo=arr[i];            
        }
            }
    System.out.println("Largest element in an array is: "+maxOne);
    System.out.println("Second Largest element in an array is: "+maxTwo);
}//end of secondLargest()
}//end of class
Please log in to add an answer.